- Home /
NavMesh Agent pause?
Hello there I was wondering what would be the best aproch to pause a navmesh agents movent towards a target.
this is what im using, I am animating a from and would like it to hop from one area to another on its own. I have it going there with a hop animation, but its linial movement, so even when stopped (animation), the object is moving and looks bad of course.
I am using this code:
GetComponent(NavMeshAgent).destination = target.position;
Just need so pause the movement about half a second or even 1... I have tried co-routines and timers, but the agent seems to override them all...
Many thanks in advance!
Cheers!
Answer by sparkzbarca · Nov 20, 2012 at 06:22 AM
Bear in mind it could be slightly buggy i've never coded in javascript just inferred by reading it on this site the language syntax.
function wait(targetPos : vector3){
GetComponent(NavMeshAgent).destination = GameObject.transform.position;
yield WaitForSeconds(1);
GetComponent(NavMeshAgent).destination = target.position;
}
mark as answered :)
Answer by Morm91 · Mar 11, 2015 at 03:07 PM
I've finally found a solution to pause and resume an agent instantly :
void pause() {
lastAgentVelocity = agent.velocity;
lastAgentPath = agent.path;
agent.velocity = Vector3.zero;
agent.ResetPath();
}
void resume() {
agent.velocity = lastAgentVelocity;
gent.SetPath(lastAgentPath);
}
This looks like a great and elegant solution. Can't wait to try it someday.
Thanks for sharing. It works pretty well; however I had some trouble resu$$anonymous$$g an agent that's paused right after start when my level reloads. (Unity 5.3.5f1)
I still don't know the exact cause of it but some race condition seemed to have reset my agent's destination before my pause() and resume(). I ended up storing the destination as well, and resets it on resume() if I found the destination is different:
void pause() {
lastAgentVelocity = agent.velocity;
lastAgentPath = agent.path;
lastAgentDestination = agent.destination;
agent.velocity = Vector3.zero;
agent.ResetPath();
}
void resume() {
if (agent.destination == lastAgentDestination) {
agent.SetPath(lastAgentPath);
}
else {
agent.SetDestination(lastAgentDestination);
}
agent.velocity = lastAgentVelocity;
}
Answer by mcroswell · Jan 16, 2013 at 11:43 PM
To pause the agent use NavMeshAgent's Stop()
method. You can use the boolean to essentially stop abruptly (true) or smoothly with avoidance (false-the default).
To resume use Resume()
and the agent continues to the current destination as you'd expect.
By the way, the above approach seems to be a valid alternative, though it might use a tad more cpu.
Hi there. I want to be able to pause and resume a navagent instantly and without deceleration, but it seems the overload Stop(bool) is now depreciated and not working as intended. And I don't find a solution anywhere.
Is there another way to do this ?
Don't really know and can't try this at the moment, but just saw your post, so wanted to ask: You did try to do the overload without the bool argument (as shown now in the reference docs)? So, I'm guessing your code is being called? I mean, the callback is still called? But just not doing what you want? Idea: Could you call Warp() on your current position, or maybe ResetPath()?
Yes, i tried without the bool argument, and it does not stop the agent instantly. Warp() and ResetPath() are not good either, because when the agent movement is resumed, i want it to have the same speed as when it stopped (i'm just trying to implement a "pause" in my game).
And what is the callback you are talking about ?
thanks @mcroswell. I did try to enable and disabling the agent and it worked too but there will be errors in the console. Stop() and Resume() method works for me. I combined it with the true false boolean and my player switches path :D
Here's how I did it in case it my be helpful to anybody.
void Update(){
OnOffNav$$anonymous$$eshAgent();
}
void OnOffNav$$anonymous$$eshAgent(){
if (nav$$anonymous$$eshToggle == true) {
transform.GetComponent<Nav$$anonymous$$eshAgent> ().Resume();
}
else if(nav$$anonymous$$eshToggle == false){
transform.GetComponent<Nav$$anonymous$$eshAgent> ().Stop();
}
}
Yeah, Stop(true) is depreciated -- but there doesn't seem to be a replacement. When called, agents slowly stop -- maybe over 1/3rd second. But they don't really "pause" at all.
I've run into this problem recently: Stop()
and Resume()
have been replaced with the property bool isStopped
. To make the Nav$$anonymous$$eshAgent
stop and start more abrutptly, increase the acceleration
variable of it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
pick random point on navmesh 4 Answers
Problem with Pause Menu to Main Menu! 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
pause menu on android phone 1 Answer