- Home /
How to stop NavMeshAgent immediately?
So I have a situation where I need the Agent to stop at the exact spot that he is in the moment I ask him to do so. I'm current using the NavMeshAgent.Stop() but he still slides about 0.1cm before actually stopping.
I noticed the old documentation had a NavMeshAgent.Stop(true):
stopUpdates If true, the GameObject is stopped immediately and not affected by the avoidance system.
But that is now deprecated. I also tried to set his velocity = Vector3.zero before stopping him but still the agent slides.
Here are my NavMeshAgent settings in case it helps:
Any help very appreciated!
Setting the velocity to zero and then calling stop used to work for me (no way to test right now), but if that doesn't works: are you calling the Stop method inside the FixedUpdate? If so, try moving it to Update ins$$anonymous$$d.
Answer by TacticalBlank · Nov 26, 2017 at 01:58 AM
I know this is old, but since all the current answers are downvoted and I don't see the correct solution here, I'll post it. To stop:
gameObject.GetComponent<NavMeshAgent>().isStopped = true;
And to start it again:
gameObject.GetComponent<NavMeshAgent>().isStopped = false;
Things like this are hacks.. Not your fault, but Unity. isStopped should be able to be created in a false mode. Ins$$anonymous$$d of it being instances in a true mode and then rushing to set it to false before anything happens.
Not really sure what you're going on about, you may be right that it's a hack, but nonetheless, it solves the problem.
You're not sure what I'm going on about and then immediately confirm you know what I'm talking about. Hmm.. Seems legit.
Answer by KellyeAnnora · Mar 31, 2020 at 05:47 PM
Just had this issue and solved it by setting the NavMeshAgent's destination to the object's current position. So basically:
gameObject.GetComponent().SetDestination(gameObject.transform.position);
Mahalo I just ran into the same issue and this fixed it right up!
@KellyeAnnora That's a very clever idea. I did that and it worked. Thanks for the tip. :)
Answer by HHess · Apr 10, 2019 at 11:11 AM
I use gameObject.GetComponent<NavMeshAgent>().velocity = Vector3.zero
I dont think that would stop the player from from moving, it would only set the nav$$anonymous$$eshAgent velocity to zero for few frames and it would start to move towards the setdestination point.
Depending on your game and your use case, setting velocity to zero every frame might work for your needs. That's what I'm doing in my game and it works great for me :)
Answer by AntonioV360 · Apr 07, 2017 at 11:30 PM
gameObject.GetComponent<NavMeshAgent>().enabled = false;
Answer by shadowpuppet · Apr 09, 2017 at 12:05 AM
I agree with Antonio. I just .enable = false the navmesh. Make sure autobraking is on , that stops the agent faster ( than NOT on) but disabling the navmesh is how I do it
Your answer
Follow this Question
Related Questions
Is it possible to store NavMeshAgents paths and assign them to other NavMeshAgents later? 1 Answer
Randomize NavMeshAgents path finding with same destination 1 Answer
how to stop agent when target is unreachable unity 1 Answer
Reasons an NavMesh Agent.isStopped might be set to True? 0 Answers
Making NavMesh areas? 0 Answers