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
11
Question by Brenden-Frank · Sep 28, 2012 at 06:22 PM · navmeshnavmeshagenteventpathingcomplete

How can I tell when a navmeshagent has reached its destination?

Looked in the documentation and couldn't find a somewhat simple solution that A* had. Is there a OnComplete() event or do I have to make one by determining remaining distance?

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

10 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by Battleb · Oct 24, 2012 at 12:42 AM

I use this: float dist=agent.remainingDistance; if (dist!=Mathf.infinite && agent.pathStatus==NavMeshPathStatus.completed && agent.remainingDistance==0) //Arrived.

You may want to check a few other conditions (pathInvalid, etc.)

Comment
Add comment · Show 4 · 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 Asvaronn · Dec 26, 2012 at 04:15 PM 0
Share

this worked for me, thank you =)

avatar image arielsan · Jan 31, 2016 at 06:38 PM 0
Share

Using only the distance have problems if there is another NavAgent with greater avoidance priority in the destination. This leads to your NavAgent to stay still but never arrive. It is an easy case to detect with the current API and you have to create special cases depending on your game, for example, if my agent is in the same position for a while, then maybe it can't reach the destination.

avatar image ZackOfAllTrades · Oct 21, 2017 at 07:52 PM 0
Share

Using .remainingDistance is bad! Please save you self some trouble and read this article.

avatar image Ziplock9000 · Feb 17, 2019 at 09:59 PM 0
Share

Be careful, this is a polling based solution, not even driven like OnComplete() which is a much better approach.

avatar image
64

Answer by Tryz · Jul 11, 2014 at 04:59 PM

You have to be careful. A distance check isn't always accurate since the steering behavior portion of the NavMeshAgent may actually still be working even if the distance is less than the stopping distance.

I found this worked best for me:

 // Check if we've reached the destination
 if (!mNavMeshAgent.pathPending)
 {
     if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)
     {
         if (!mNavMeshAgent.hasPath || mNavMeshAgent.velocity.sqrMagnitude == 0f)
         {
             // Done
         }
     }
 }


Comment
Add comment · Show 12 · 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 laryk_ua · Sep 11, 2014 at 10:56 AM 0
Share

Thanks, Tryz!!! That worked much better in comparison with Battleb answer

avatar image challlen · Sep 30, 2014 at 01:36 AM 0
Share

Thank you!

avatar image Nightmare_82 · Dec 04, 2014 at 06:39 PM 0
Share

Thanks, works perfectly!

avatar image Nikunj-Kareliya · Dec 04, 2015 at 10:26 AM 3
Share

Hi @Tryz, sometimes inner most if statement executes 2 times, so target variable value increments 2 times consecutively.

 if (!mNav$$anonymous$$eshAgent.pathPending)
  {
      if (mNav$$anonymous$$eshAgent.remainingDistance <= mNav$$anonymous$$eshAgent.stoppingDistance)
      {
          if (!mNav$$anonymous$$eshAgent.hasPath || mNav$$anonymous$$eshAgent.velocity.sqr$$anonymous$$agnitude == 0f)
          {
                // Done
                Debug.Log("Executes 2 times");
                if(targetPoint >= points.Length - 1) { // if it's a last point
                    targetPoint = 0;
         }
         else {
             targetPoint++;
         }
 
          }
      }
  }

Any help would be appreciated.

avatar image TKDHayk · Aug 02, 2016 at 12:35 AM 1
Share

This works if its in the Update function, but im trying to avoid making calculation every frame. Is there a way to Run a piece of code ONLY when the agent reaches the destination? thanks!

avatar image Maloke TKDHayk · Jul 25, 2017 at 11:03 PM 2
Share

You could create a Coroutine with this code inside and call it at the moment you set the desired position to move. (I know the question is old but it might be a useful hint for others)

Show more comments
avatar image
5

Answer by JimRustler · Oct 03, 2014 at 09:12 AM

In the Unity Documentation it says that remainingDistance is "The distance between the agent's position and the destination on the current path". I kept getting a zero for remaining distance so my workaround pathcomplete function was a bit different

 protected bool pathComplete()
     {
         if ( Vector3.Distance( m_NavAgent.destination, m_NavAgent.transform.position) <= m_NavAgent.stoppingDistance)
         {
             if (!m_NavAgent.hasPath || m_NavAgent.velocity.sqrMagnitude == 0f)
             {
                 return true;
             }
         }
 
         return false;
     }

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 WoodcockCreative · May 26, 2017 at 09:24 PM 0
Share

I was having the same prople JimRustler. Thanks for this version. Simple and leaves nothing to chance.

avatar image lejean · Feb 01, 2018 at 04:09 PM 0
Share

You should use remainingDistance. Vector3.distance just draws a straight line between the 2 objects and calculates the distance.

When there's a wall between the 2 for example, Vector3.distance is no longer accurate.

avatar image
4

Answer by Billu · Mar 25, 2016 at 11:21 AM

One way to implement this mechanism is to place a GameObject on the destination (you probably used one as destination already), and add BoxCollider and a trigger. Once the object with the nav mesh agent collided with the destination game object, you can determine in the OnTriggerEnter() function and check if the colliding object is the one you want (use tag or name), and do your rest stuff.

Of course, you can attach a script on the destination object, so it will become transparent on game start, so no one would see them.

Comment
Add comment · Show 3 · 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 soxroxr · May 27, 2016 at 08:31 PM 0
Share

This is a wonderful and simple solution.

avatar image shadowpuppet · Jan 08, 2017 at 10:40 PM 0
Share

yes that certainly is one way. I have been doing that for some time now but it feels like such a hack to me( I am a noob) so I was looking for simply ( or not so simply) telling the navmesh agent that it has reached its stopping DIstance so ...stop this animation and start another. Sounds easy when I say it but can't get it to work at all. Nor can I get ANY of the solutions on this page to work. is this java or what? I get errors on all sorts of stuff. why can't it be as simple as "if( remaingDistance ==0){ animation .Play("whatever")

avatar image z-schwarz · Jan 25, 2021 at 08:54 PM 0
Share

@Billu $$anonymous$$y units now patrolling. And if another enemy unit comes into their predefined range - while they patrol - I want them to automatically attack them. Would It be a good idea to solve this also with a Collider? I mean to have 2 Collider on the gameObject. One for it's aggro-range, and another one to deter$$anonymous$$e how close the unit has to get to attack the target?

avatar image
1

Answer by justinl · Oct 14, 2012 at 04:14 AM

This isn't much of an answer, but for other people who have the same question, yes Brenden you are correct. You'll need to write your own. It might also be worth noting that sometimes myAgent.remainingDistance is not always accurate or available, so you may need to resort to using Vector3.Distance() as a backup.

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
  • 1
  • 2
  • ›

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

39 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I limit the path of a NavMeshAgent? 0 Answers

Finding Closest Object Through Navmesh,Identifying Which Object Is Closest on NavMesh 0 Answers

Instantiated objects at beginning of runtime are messing with the pathing for my NavMesh. 0 Answers

Is it possible to have a navmesh agent that ignores obstacles? 1 Answer

Can't get NavMeshAgent move. NavMeshAgent ignores destination property 7 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