I want to check velocity of the moving agent and apply the animation according to its current velocity magnitude (walking, running, idle etc…). Normally, I use rigidbody.velocity but this doesn’t work with NavMeshAgent movement. Is there a way to check agent’s current velocity for NavMeshAgent movement?
You can also check if (nav.velocity != Vector3.zero) which will show movement if true.
0-1 velocity:
float velocity = agent.velocity.magnitude/agent.speed;
i use this for blendtree based animations
I just keep track of the transform.position every frame, and use the difference to determine speed.
private Vector3 previousPosition;
public float curSpeed;
void Update()
{
Vector3 curMove = transform.position - previousPosition;
curSpeed = curMove.magnitude / Time.deltaTime;
previousPosition = transform.position;
}
i found this to be a stable alternative finding the predicted speed
float speed = 0f; speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude;
taken from the unity demo
nav.velocity.x
and
nav.velocity.z
but the velocity is in world space.