Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RudeFX · Nov 20, 2012 at 04:05 AM · navmeshpause

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
-2
Best Answer

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

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);
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mcroswell · Mar 11, 2015 at 11:03 PM 0
Share

This looks like a great and elegant solution. Can't wait to try it someday.

avatar image caroparo · Jul 31, 2016 at 06:16 AM 0
Share

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;
 }
 
avatar image
6

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Morm91 · Mar 11, 2015 at 12:29 AM 1
Share

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 ?

avatar image mcroswell · Mar 11, 2015 at 01:44 AM 0
Share

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()?

avatar image Morm91 · Mar 11, 2015 at 10:04 AM 0
Share

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 ?

avatar image exorakhilas · Oct 26, 2015 at 08:51 AM 0
Share

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();
         }
 }
avatar image infinitypbr · Jan 24, 2016 at 10:01 PM 0
Share

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.

avatar image Harinezumi infinitypbr · Mar 26, 2019 at 09:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Pause menu scripting help? 1 Answer

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

navmesh ai check if ai is looking at you 1 Answer

Pausing and unpausing the game with one button 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges