quakeball/Assets/Scripts/Animation/SoldierProceduralAnimator.cs

72 lines
2.7 KiB
C#

using UnityEngine;
using NeonTea.Quakeball.Players;
namespace NeonTea.Quakeball.Animation {
/// <summary>Animates the parts that can't be animated via the animation system, e.g. aiming.</summary>
public class SoldierProceduralAnimator : MonoBehaviour {
public Player Player;
public Animator Soldier;
public Transform HeadCollider;
private Transform Body;
private Transform Torso;
private Transform Head;
private Vector3 BodyBaseEulers;
private Vector3 TorsoBaseEulers;
private Vector3 HeadBaseEulers;
private Vector3 BodyEulers;
private Vector3 TorsoEulers;
private Vector3 HeadEulers;
private float BodyYaw = 0;
private void Awake() {
Body = Soldier.transform;
Torso = GameObject.FindGameObjectWithTag("PlayerTorso").transform;
Head = GameObject.FindGameObjectWithTag("PlayerHead").transform;
HeadCollider.parent = Head;
BodyEulers = BodyBaseEulers = Body.localEulerAngles;
TorsoEulers = TorsoBaseEulers = Torso.localEulerAngles;
HeadEulers = HeadBaseEulers = Head.localEulerAngles;
}
private void Update() {
Soldier.SetFloat("Movement", Player.GroundVelocity.magnitude / Player.MoveStyle.TargetVelocity);
}
private void LateUpdate() {
if (Player.MoveDirection.magnitude > 0) {
float NewBodyYaw = Vector3.SignedAngle(Vector3.forward, Player.MoveDirection, Vector3.up);
if (BodyYaw < -90 && NewBodyYaw > 90) {
BodyEulers += new Vector3(0, 360, 0);
}
if (BodyYaw > 90 && NewBodyYaw < -90) {
BodyEulers -= new Vector3(0, 360, 0);
}
BodyYaw = NewBodyYaw;
}
float Yaw = Player.Yaw - BodyYaw;
while (Yaw < -180) {
Yaw += 360;
}
while (Yaw > 180) {
Yaw -= 360;
}
Vector3 TargetBodyEulers = BodyBaseEulers + new Vector3(0, BodyYaw, 0);
BodyEulers = Vector3.Lerp(BodyEulers, TargetBodyEulers, 10f * Time.deltaTime);
Body.localEulerAngles = BodyEulers;
Vector3 TargetTorsoEulers = TorsoBaseEulers + new Vector3(0, Yaw, 0);
TorsoEulers = Vector3.Lerp(TorsoEulers, TargetTorsoEulers, 10f * Time.deltaTime);
Torso.localEulerAngles = TorsoEulers;
Vector3 TargetHeadEulers = HeadBaseEulers + new Vector3(-Player.Pitch, 0, 0);
HeadEulers = Vector3.Lerp(HeadEulers, TargetHeadEulers, 10f * Time.deltaTime);
Head.localEulerAngles = HeadEulers;
}
}
}