quakeball/Assets/Scripts/Animation/SoldierProceduralAnimator.cs
2020-08-10 00:50:59 +03:00

59 lines
2.1 KiB
C#

using UnityEngine;
using NeonTea.Quakeball.Players;
using NeonTea.Quakeball.Util;
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 enum SoldierModel {
Female = 0,
Male = 1,
}
public Animator[] Soldiers;
public SoldierModel Model;
public Player Player;
public Transform HeadCollider;
[Header("Gun holding")]
public bool GunGluedToHand = true;
public Transform Gun;
public Transform GunHandle;
private Animator Animator;
private float BodyYaw = 0;
private Transform BehindHand;
private Transform FrontHand;
private void Awake() {
foreach (Animator animator in Soldiers) {
animator.gameObject.SetActive(false);
}
Animator = Soldiers[(int)Model].GetComponent<Animator>();
Animator.gameObject.SetActive(true);
Transform Head = TransformUtil.FindChildWithName(Animator.transform, "HEAD");
HeadCollider.parent = Head;
BehindHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.R");
FrontHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.L");
}
private void Update() {
Animator.SetFloat("Movement", Player.GroundVelocity.magnitude / Player.MoveStyle.TargetVelocity);
}
private void LateUpdate() {
if (Player.GroundVelocity.magnitude > 0) {
BodyYaw = Vector3.SignedAngle(Vector3.forward, Player.GroundVelocity.normalized, Vector3.up);
}
transform.localEulerAngles = new Vector3(0, BodyYaw, 0);
if (GunGluedToHand) {
Vector3 GunOffset = GunHandle.position - Gun.position;
Vector3 GunDirection = (FrontHand.position - BehindHand.position).normalized;
Gun.position = BehindHand.position - GunOffset;
Gun.LookAt(Gun.position + GunDirection);
}
}
}
}