quakeball/Assets/Scripts/Animation/SoldierProceduralAnimator.cs

59 lines
2.1 KiB
C#
Raw Normal View History

2020-08-08 20:02:17 +02:00
using UnityEngine;
using NeonTea.Quakeball.Players;
2020-08-09 19:33:23 +02:00
using NeonTea.Quakeball.Util;
2020-08-08 20:02:17 +02:00
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 {
2020-08-09 19:33:23 +02:00
public enum SoldierModel {
Female = 0,
Male = 1,
}
public Animator[] Soldiers;
public SoldierModel Model;
2020-08-08 20:02:17 +02:00
public Player Player;
2020-08-08 21:30:13 +02:00
public Transform HeadCollider;
2020-08-08 20:02:17 +02:00
2020-08-09 19:33:23 +02:00
[Header("Gun holding")]
public bool GunGluedToHand = true;
public Transform Gun;
public Transform GunHandle;
2020-08-08 20:02:17 +02:00
2020-08-09 19:33:23 +02:00
private Animator Animator;
2020-08-08 21:30:13 +02:00
private float BodyYaw = 0;
2020-08-09 19:33:23 +02:00
private Transform BehindHand;
private Transform FrontHand;
2020-08-08 21:30:13 +02:00
2020-08-08 20:02:17 +02:00
private void Awake() {
2020-08-09 19:33:23 +02:00
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");
2020-08-08 21:30:13 +02:00
HeadCollider.parent = Head;
2020-08-09 19:33:23 +02:00
BehindHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.R");
FrontHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.L");
2020-08-08 20:02:17 +02:00
}
2020-08-08 21:30:13 +02:00
private void Update() {
2020-08-09 19:33:23 +02:00
Animator.SetFloat("Movement", Player.GroundVelocity.magnitude / Player.MoveStyle.TargetVelocity);
2020-08-08 21:30:13 +02:00
}
2020-08-08 20:02:17 +02:00
private void LateUpdate() {
2020-08-09 19:33:23 +02:00
if (Player.GroundVelocity.magnitude > 0) {
BodyYaw = Vector3.SignedAngle(Vector3.forward, Player.GroundVelocity.normalized, Vector3.up);
2020-08-08 21:30:13 +02:00
}
2020-08-09 19:33:23 +02:00
transform.localEulerAngles = new Vector3(0, BodyYaw, 0);
2020-08-08 21:30:13 +02:00
2020-08-09 19:33:23 +02:00
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);
}
2020-08-08 20:02:17 +02:00
}
}
}