101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
using FlaxEngine;
|
|
|
|
namespace Game;
|
|
|
|
public class CameraMovement : Script
|
|
{
|
|
private float viewPitch;
|
|
private float viewRoll;
|
|
private float viewYaw;
|
|
|
|
[Limit(0, 9000)]
|
|
[Tooltip("Camera speed")]
|
|
public float MoveSpeed { get; set; } = 400;
|
|
|
|
[ConsoleVariable("sensitivity")]
|
|
private static float _sensitivity { get; set; } = 1.0f;
|
|
|
|
public override void OnStart()
|
|
{
|
|
Float3 initialEulerAngles = Actor.Orientation.EulerAngles;
|
|
viewPitch = initialEulerAngles.X;
|
|
viewYaw = initialEulerAngles.Y;
|
|
viewRoll = initialEulerAngles.Z;
|
|
}
|
|
|
|
public override void OnUpdate()
|
|
{
|
|
Transform camTrans = Actor.Transform;
|
|
Actor rootActor = Actor.GetChild(0);
|
|
Camera camera = rootActor.GetChild<Camera>();
|
|
|
|
float xAxis = Input.GetAxisRaw("Mouse X") * _sensitivity;
|
|
float yAxis = Input.GetAxisRaw("Mouse Y") * _sensitivity;
|
|
if (xAxis != 0.0f || yAxis != 0.0f)
|
|
{
|
|
viewPitch += yAxis;
|
|
viewYaw += xAxis;
|
|
|
|
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
|
|
|
|
// root orientation must be set first
|
|
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
|
|
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
|
|
}
|
|
|
|
float inputH = Input.GetAxis("Horizontal");
|
|
float inputV = Input.GetAxis("Vertical");
|
|
Float3 move = new Float3(inputH, 0.0f, inputV);
|
|
|
|
if (!move.IsZero)
|
|
{
|
|
move.Normalize();
|
|
move = camera.Transform.TransformDirection(move) * MoveSpeed;
|
|
|
|
{
|
|
Float3 delta = move * Time.UnscaledDeltaTime;
|
|
float movementLeft = delta.Length;
|
|
|
|
// TODO: check multiple times in case we get stuck in walls
|
|
|
|
float sphereRadius = 10.0f; // TODO: use collider radius
|
|
RayCastHit[] hitInfos;
|
|
float moveDist = delta.Length;
|
|
Physics.SphereCastAll(Actor.Transform.Translation, sphereRadius, move.Normalized, out hitInfos,
|
|
moveDist);
|
|
|
|
//bool nohit = true;
|
|
float hitDistance = moveDist;
|
|
Float3 hitNormal = move.Normalized;
|
|
foreach (RayCastHit hitInfo in hitInfos)
|
|
{
|
|
if (hitInfo.Collider.Parent == Parent)
|
|
continue;
|
|
|
|
if (hitInfo.Distance < hitDistance)
|
|
{
|
|
hitDistance = hitInfo.Distance;
|
|
hitNormal = hitInfo.Normal;
|
|
}
|
|
//nohit = false;
|
|
//break;
|
|
}
|
|
|
|
if (hitDistance != moveDist)
|
|
//camTrans.Translation = Float3.Lerp(Actor.Transform.Translation, camTrans.Translation, hitDistance);
|
|
|
|
//projected = normal * dot(direction, normal);
|
|
//direction = direction - projected
|
|
|
|
//camTrans.Translation += hitNormal * (moveDist - hitDistance); // correct?
|
|
//camTrans.Translation = hitNormal * (move * hitNormal); // correct?
|
|
//camTrans.Translation = Actor.Transform.Translation;
|
|
delta += -Float3.Dot(delta, hitNormal) * hitNormal; // correct?
|
|
|
|
camTrans.Translation += delta;
|
|
}
|
|
}
|
|
|
|
Actor.Transform = camTrans;
|
|
}
|
|
} |