quakeball/Assets/Scripts/Animation/SoldierProceduralAnimator.cs

69 lines
2.7 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;
private Transform BehindHand;
private Transform FrontHand;
2020-08-10 01:42:55 +02:00
private Transform Torso;
private float VisualYaw;
private float VisualPitch;
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);
2020-08-10 01:42:55 +02:00
HeadCollider.parent = TransformUtil.FindChildWithName(Animator.transform, "HEAD");
Torso = TransformUtil.FindChildWithName(Animator.transform, "RIBS");
2020-08-09 19:33:23 +02:00
BehindHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.R");
FrontHand = TransformUtil.FindChildWithName(Animator.transform, "HAND.L");
2020-08-10 00:16:06 +02:00
if (GunGluedToHand) {
Gun.parent = Animator.transform;
}
2020-08-08 20:02:17 +02:00
}
2020-08-08 21:30:13 +02:00
private void Update() {
2020-08-10 00:16:06 +02:00
float Right = Vector3.Dot(Player.GroundVelocity, transform.right) / Player.MoveStyle.TargetVelocity / 0.7f;
float Forward = Vector3.Dot(Player.GroundVelocity, transform.forward) / Player.MoveStyle.TargetVelocity / 0.7f;
Animator.SetFloat("Forward", Mathf.Clamp(Forward, -1, 1));
Animator.SetFloat("Right", Mathf.Clamp(Right, -1, 1));
2020-08-08 21:30:13 +02:00
}
2020-08-08 20:02:17 +02:00
private void LateUpdate() {
2020-08-10 01:42:55 +02:00
VisualYaw = Mathf.Lerp(VisualYaw, Player.Yaw, 10f * Time.deltaTime);
VisualPitch = Mathf.Lerp(VisualPitch, -Player.Pitch, 10f * Time.deltaTime);
transform.localEulerAngles = new Vector3(0, VisualYaw, 0);
Torso.localEulerAngles = new Vector3(VisualPitch, 0, 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
}
}
}