Getting Speed of an agent for animator updates

Hello, i am trying to obtain the speed/ velocity.magnitude for my agent to update my animator component for movement values but it seems like once the agent has stopped moving the velocity no longer updates and is stuck on the last value

Bellow is a screenshot of issue, im reading the Velocity directly from the AIPath component and storing it locally in my AniamtorController script and obtaining the magnitude which i use to drive the animations of my characters but it seems when the agent reaches its destination it doesnt actually seem to update the velocity in the AIPAth script??? am i doing something wrong for obtaining velocity here?

  [SerializeField] private AIPath ai;

 private void Update()
        {
            velocity = ai.velocity;

            if (ai.velocity.magnitude > 0.1f)
                speed = ai.velocity.magnitude;
            else
                speed = 0;

            animator.SetFloat("Z", speed);
        }

Solved, had a look at some other threads here and got some ideas, wasnt aware their were demo scenes in the package so had a quick look through those and seen a robot animator. seems like gravity is also applied to the velocity of sorts so we need to remove the Y factor. Here is my updated and working animation controller hooked in with Behaviour Designer and A* Pathfinding

namespace RealSoftGames
{
    public class AnimatorController : MonoBehaviour
    {
        private Vector3 relVelocity;
        [SerializeField, ReadOnly] private Transform parentTransform;
        [SerializeField, ReadOnly] private Animator animator;
        [SerializeField, ReadOnly] private IAstarAI ai;
        [SerializeField, ReadOnly] private float speed;

        private void Start()
        {
            ai = GetComponentInParent<IAstarAI>();
            parentTransform = transform.parent;
            animator = gameObject.GetComponent<Animator>();
        }

        private void Update()
        {
            relVelocity = parentTransform.InverseTransformDirection(ai.velocity);
            relVelocity.y = 0;
            speed = relVelocity.magnitude;

            animator.SetFloat("Z", speed);
        }
    }
}

And a screen shot for those that want to see how my AI is hooked up in the inspector

1 Like