Player/PlayerInteractor.cs
using Sandbox;
public sealed class PlayerInteractor : Component
{
[Property] private GameObject Eye { get; set; }
public TimeSince timeSinceCarriedObject = 1;
GameObject carry;
public GameObject currentlyCarriedObject
{
get => carry;
set
{
carry = value;
}
}
private Vector3 localMassCenter;
[Property]
public Opium.PlayerController PlayerController { get; set; }
public void PlayerInteractorInput()
{
var tr = Scene.PhysicsWorld.Trace.Ray( Eye.WorldPosition, Eye.WorldPosition + Eye.WorldRotation.Forward * 500 )
.WithoutTags( "player" )
.Run();
if ( Input.Pressed( "use" ) && !currentlyCarriedObject.IsValid() )
{
if ( tr.Body.IsValid() && tr.Body.GetGameObject().Tags.Has( "carry" ) )
{
var interactedObject = tr.Body.GetGameObject();
var interactComponent = interactedObject.Components.Get<ModelRenderer>();
var physicsComponent = interactedObject.Components.Get<Rigidbody>();
//Poopy HOHO BEN
//if( physicsComponent.PhysicsBody.Mass > 500 ) return;
if ( interactComponent.IsValid() && physicsComponent.IsValid() )
{
// Pick up the object
currentlyCarriedObject = interactedObject;
// Keep the mass center to remove it from target position
localMassCenter = tr.Body.LocalMassCenter;
targetRotation = Eye.WorldRotation.Inverse * physicsComponent.WorldRotation;
HoldRot = physicsComponent.WorldRotation;
}
}
}
else if( Input.Pressed( "use" ) && currentlyCarriedObject.IsValid() )
{
// Release the object
currentlyCarriedObject = null;
PlayerController.IsHoldingObject = false;
}
if ( Input.Pressed( "attack1" ) && currentlyCarriedObject.IsValid() )
{
timeSinceCarriedObject = 0;
//throw the object
var physicsBody = currentlyCarriedObject.Components.Get<Rigidbody>();
physicsBody.Velocity = Eye.WorldRotation.Forward * physicsBody.PhysicsBody.Mass.Remap( 0, 1000, 400, 10 );
physicsBody.AngularVelocity = Vector3.Zero;
// Release the object
currentlyCarriedObject = null;
PlayerController.IsHoldingObject = false;
}
}
Rotation targetRotation;
Angles HoldRot;
protected override void OnFixedUpdate()
{
base.OnFixedUpdate();
if ( !currentlyCarriedObject.IsValid() )
return;
var physicsBody = currentlyCarriedObject.Components.Get<Rigidbody>();
if ( !physicsBody.IsValid() )
return;
// Target position is a distance away from the eye, minus center of mass offset
var currentPosition = physicsBody.Transform.Position;
var eyeClamp = Eye.Transform.Rotation.Angles();
eyeClamp.pitch = eyeClamp.pitch.Clamp(-40,40);
var targetPosition = Eye.Transform.Position + eyeClamp.Forward * 50;
targetPosition -= physicsBody.Transform.Rotation * localMassCenter;
// Calculate the velocity needed to move from current to target position
var velocity = physicsBody.Velocity;
Vector3.SmoothDamp( currentPosition, targetPosition, ref velocity, 0.2f, Time.Delta );
physicsBody.Velocity = velocity;
// Add the eye rotation back onto the local rotation to make it a global rotation
var currentRotation = physicsBody.Transform.Rotation;
HoldRot = eyeClamp * targetRotation;
// Calculate the velocity needed to move from current to target rotation
var angvelocity = physicsBody.AngularVelocity;
Rotation.SmoothDamp( currentRotation, HoldRot, ref angvelocity, 0.075f, Time.Delta );
physicsBody.AngularVelocity = angvelocity;
}
}