ZeroVR/ZeroPacientVR/Assets/Scripts/Npc/NpcMovementAnimator.cs

18 lines
522 B
C#

using UnityEngine;
using UnityEngine.AI;
public class NpcMovementAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
[SerializeField] private NavMeshAgent agent;
private static readonly int SpeedAnim = Animator.StringToHash("Speed");
private void Update()
{
var speed = agent.velocity.magnitude / (agent.speed > 0 ? agent.speed : 1f);
if (agent.speed == 0) speed = 0;
speed = Mathf.Clamp(speed, 0f, 1f);
animator.SetFloat(SpeedAnim, speed);
}
}