- Home /
Is there a way to check agent velocity for NavMeshAgent movement?
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?
Answer by syclamoth · May 14, 2012 at 10:37 AM
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;
}
omg Thank you so very much, I tried doing this exact same thing but in such a messy and complicated way. Upon reading your code and in hindsight it seems incredibly obvious and simple, yet I managed to butcher it! Thank You again! $$anonymous$$y object does not have a Rigidbody so I cant really get the velocity to predict the direction.
Answer by AlexTudo · Feb 21, 2015 at 08:28 PM
You can also check if (nav.velocity != Vector3.zero) which will show movement if true.
Thank you so much! You made my life a LOT easier!
Answer by kristoof · Aug 07, 2017 at 12:59 PM
0-1 velocity:
float velocity = agent.velocity.magnitude/agent.speed;
i use this for blendtree based animations
This is the best answer for me because you can set what happen at a certain velocity, rather than using it as a bool like the other answers.
agent.velocity.magnitude/agent.speed; <--- Gives you a percentage of the maximum agent speed agent.velocity.magnitude; <--- Gives you the current speed
Answer by wethecom · Mar 01, 2016 at 08:13 AM
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 https://unity3d.com/learn/tutorials/projects/stealth/enemy-animation
Answer by pokeyoshi3 · May 31, 2016 at 07:07 PM
nav.velocity.x
and
nav.velocity.z
but the velocity is in world space.
Your answer
Follow this Question
Related Questions
How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer
error CS0019: trying to increase velocity when key is pressed twice in quick succession 1 Answer
Stop A player Turning at Specific Point. 1 Answer
How to make a NavMeshAgent move off another gameobject below it? 1 Answer
How to make a gamebject wiggle slowly as it moves forward ? 1 Answer