smoothen camera in stairs
This commit is contained in:
71
Source/Game/CameraSpring.cs
Normal file
71
Source/Game/CameraSpring.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Runtime;
|
||||
using FlaxEditor.Content.Settings;
|
||||
using FlaxEngine;
|
||||
using Console = Cabrito.Console;
|
||||
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class CameraSpring : Script
|
||||
{
|
||||
private Vector3 targetOffset;
|
||||
private Vector3 lastPosition;
|
||||
|
||||
public float speed = 240f;
|
||||
public float percY;
|
||||
|
||||
private Actor playerActor;
|
||||
private Actor viewModelHolder;
|
||||
private PlayerMovement playerMovement;
|
||||
|
||||
private bool lastGround;
|
||||
|
||||
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(Vector3 position)
|
||||
{
|
||||
Actor.Position = position;
|
||||
viewModelHolder.Position = position;
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
Vector3 position = Actor.Parent.Position + targetOffset;
|
||||
Vector3 targetPosition = position;
|
||||
|
||||
if (playerMovement.onGround)
|
||||
{
|
||||
float deltaY = position.Y - lastPosition.Y;
|
||||
//if (Mathf.Abs(deltaY) < 10f)
|
||||
if (deltaY > 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user