quakeball/Assets/Scripts/Animation/SoldierProceduralAnimator.cs

72 lines
2.7 KiB
C#
Raw Normal View History

2020-08-08 20:02:17 +02:00
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;
2020-08-08 21:30:13 +02:00
public Animator Soldier;
public Transform HeadCollider;
2020-08-08 20:02:17 +02:00
2020-08-08 21:30:13 +02:00
private Transform Body;
private Transform Torso;
private Transform Head;
private Vector3 BodyBaseEulers;
2020-08-08 20:02:17 +02:00
private Vector3 TorsoBaseEulers;
private Vector3 HeadBaseEulers;
2020-08-08 21:30:13 +02:00
private Vector3 BodyEulers;
2020-08-08 20:02:17 +02:00
private Vector3 TorsoEulers;
private Vector3 HeadEulers;
2020-08-08 21:30:13 +02:00
private float BodyYaw = 0;
2020-08-08 20:02:17 +02:00
private void Awake() {
2020-08-08 21:30:13 +02:00
Body = Soldier.transform;
Torso = GameObject.FindGameObjectWithTag("PlayerTorso").transform;
Head = GameObject.FindGameObjectWithTag("PlayerHead").transform;
HeadCollider.parent = Head;
BodyEulers = BodyBaseEulers = Body.localEulerAngles;
2020-08-08 20:02:17 +02:00
TorsoEulers = TorsoBaseEulers = Torso.localEulerAngles;
HeadEulers = HeadBaseEulers = Head.localEulerAngles;
}
2020-08-08 21:30:13 +02:00
private void Update() {
Soldier.SetFloat("Movement", Player.GroundVelocity.magnitude / Player.MoveStyle.TargetVelocity);
}
2020-08-08 20:02:17 +02:00
private void LateUpdate() {
2020-08-08 21:30:13 +02:00
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;
2020-08-08 20:02:17 +02:00
while (Yaw < -180) {
Yaw += 360;
}
while (Yaw > 180) {
Yaw -= 360;
}
2020-08-08 21:30:13 +02:00
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);
2020-08-08 20:02:17 +02:00
TorsoEulers = Vector3.Lerp(TorsoEulers, TargetTorsoEulers, 10f * Time.deltaTime);
Torso.localEulerAngles = TorsoEulers;
2020-08-08 21:30:13 +02:00
Vector3 TargetHeadEulers = HeadBaseEulers + new Vector3(-Player.Pitch, 0, 0);
2020-08-08 20:02:17 +02:00
HeadEulers = Vector3.Lerp(HeadEulers, TargetHeadEulers, 10f * Time.deltaTime);
Head.localEulerAngles = HeadEulers;
}
}
}