quakeball/Assets/Scripts/Animation/GunInterpolator.cs

42 lines
1.6 KiB
C#
Raw Permalink Normal View History

2020-08-06 21:58:31 +02:00
using UnityEngine;
2020-08-07 20:24:43 +02:00
using NeonTea.Quakeball.Players;
2020-08-06 21:58:31 +02:00
namespace NeonTea.Quakeball.Animation {
/// <summary>Lags a little behind every frame to create a delay effect in the gun movement.</summary>
public class GunInterpolator : MonoBehaviour {
public LocalPlayer Player;
public float InterpolationSpeed;
public float YawDegrees;
public float PitchDegrees;
public float RollDegrees;
public float RotationThreshold;
private Vector2 CurrentLean = Vector2.zero;
private Vector2 TargetLean = Vector2.zero;
private void Update() {
Vector2 LookDelta = Player.LookAction.ReadValue<Vector2>();
SetTarget(LookDelta.x, ref TargetLean.x);
CurrentLean.x = Mathf.Lerp(CurrentLean.x, TargetLean.x, InterpolationSpeed * Time.deltaTime);
SetTarget(LookDelta.y, ref TargetLean.y);
CurrentLean.y = Mathf.Lerp(CurrentLean.y, TargetLean.y, InterpolationSpeed * Time.deltaTime);
Vector3 Eulers = transform.localEulerAngles;
Eulers.x = -CurrentLean.y * PitchDegrees;
Eulers.y = CurrentLean.x * YawDegrees;
Eulers.z = CurrentLean.x * RollDegrees;
transform.localEulerAngles = Eulers;
}
private void SetTarget(float delta, ref float targetLean) {
if (delta > RotationThreshold) {
targetLean = -1;
} else if (delta < -RotationThreshold) {
targetLean = 1;
} else {
targetLean = Mathf.Lerp(targetLean, 0, 10f * Time.deltaTime);
}
}
}
}