75 lines
2.3 KiB
C#
75 lines
2.3 KiB
C#
using FlaxEngine;
|
|
|
|
namespace Game
|
|
{
|
|
public class CameraSpring : Script
|
|
{
|
|
private bool lastGround;
|
|
private Float3 lastPosition;
|
|
public float percY;
|
|
|
|
private Actor playerActor;
|
|
private PlayerMovement playerMovement;
|
|
|
|
public float speed = 240f;
|
|
private Float3 targetOffset;
|
|
private Actor viewModelHolder;
|
|
|
|
public override void OnStart()
|
|
{
|
|
playerActor = Actor.Parent.Parent;
|
|
playerMovement = playerActor.GetScript<PlayerMovement>();
|
|
viewModelHolder = playerActor.GetChild("ViewModelHolder");
|
|
|
|
lastGround = playerMovement.onGround;
|
|
targetOffset = Actor.LocalPosition;
|
|
}
|
|
|
|
private void UpdatePosition(Float3 position)
|
|
{
|
|
Actor.Position = position;
|
|
viewModelHolder.Position = position;
|
|
}
|
|
|
|
public override void OnUpdate()
|
|
{
|
|
Float3 position = Actor.Parent.Position + targetOffset;
|
|
Float3 targetPosition = position;
|
|
|
|
if (playerMovement.onGround)
|
|
{
|
|
float deltaY = position.Y - lastPosition.Y;
|
|
//if (Mathf.Abs(deltaY) < 10f)
|
|
if (deltaY > 0)
|
|
{
|
|
if (deltaY > 100f)
|
|
{
|
|
// Teleported, snap instantly
|
|
UpdatePosition(position);
|
|
}
|
|
else
|
|
{
|
|
const float catchUpDistance = 10f;
|
|
const float catchUpMinMultip = 0.25f;
|
|
percY = Mathf.Abs(deltaY) / catchUpDistance;
|
|
percY = Mathf.Min(1.0f, percY + catchUpMinMultip);
|
|
percY *= percY;
|
|
|
|
float adjustSpeed = speed * Time.DeltaTime * percY;
|
|
|
|
position.Y = lastPosition.Y; //-= deltaY;
|
|
position.Y = Mathf.MoveTowards(position.Y, targetPosition.Y, adjustSpeed);
|
|
UpdatePosition(position);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UpdatePosition(position);
|
|
}
|
|
|
|
lastPosition = position;
|
|
lastGround = playerMovement.onGround;
|
|
}
|
|
}
|
|
} |