Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Wendek · Nov 22, 2013 at 03:33 PM · c#navmeshnavmeshagentnavigation

NavmeshAgent.remainingDistance remains at 0 but the agent still moves and behaves correctly

Hello,

I'm having a rather weird bug with Unity's navmesh system (I'm using Unity Free 4.2). Basically, the navmesh system itself works correctly, the unit goes from A to B and avoids the obstacles. However, sometimes if I try to log the remainingDistance variable as it moves it stays at zero the whole time. This is slightly annoying because I want to know whether the unit is moving so that I can draw its selectionbox at the right position (this is for a RTS game), and of course I'm using the "remainingDistance" variable to know when the unit has reached its destination. What really confuses me is that for two identical units (same prefab, same script, same everything as far as I can tell), one will have the correct variable and the other one won't. Even weirder, the units who are "bugged" still behave normally if I don't let them stop. What I mean is, I tell unit 1 to go from A to B. It will bug out and say that remainingDistance is 0 despite it clearly moving (let me reiterate that even when I say a unit is "bugged", it still moves around as expected so it's not like the navmesh system itself doesn't work). If I wait until it reaches B and then tell it to go to C, the same will happen -except the position will be recalculated once, at the time of the click-. But if I give it another order mid-moving, then suddenly it starts working normally. So if I keep giving it new orders it somehow works, but if it ever stops once, then it's going to bug again until I give two orders in a row.

Now there are obviously hacky ways to solve it, such as treating any click as a double-click or issuing the command twice if the unit was not moving before. But it's a dirty hack and I'd rather solve the problem than hide it with a hack. If it's possible to solve it, that is.

Thanks in advance

Comment
Add comment · Show 3
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 jimjames · Feb 16, 2015 at 01:51 PM 0
Share

Was wondering if you have solved this problem? I am also interested in the solution as I have the same problem with my project with the "remainingDistance" variable.

avatar image Wendek · Feb 26, 2015 at 09:12 PM 0
Share

Wow you must have looked for quite some time to find that forsaken post. Unfortunately, nope I never truly solved this problem. Then again I haven't worked on that project of $$anonymous$$e for over a year. A bit concerning to see that the issue is still present with the newer versions, though I suspect it's the kind of bug that's also very hard to track down.

Basically I think your best bet is to implement a small hack, either one of those I mentioned in the original post or find a new (perhaps cleaner) one.

Please report on this thread if you ever find a definitive solution though!

avatar image Ziron999 · Mar 19, 2015 at 11:10 PM 0
Share

from my understanding remainingdistance is 0 because it is hitting every path if you get the object stuck then it should become higher than 0.

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Ssential · Nov 02, 2019 at 06:41 PM

I've encountered the same problem and found another solution: The NavMeshAgent has a property called pathPending which becomes false once the calculation of the path has finished. So inside the Update Function I check for pathPending and only continue once pathPending is false.

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
2

Answer by smalldreamworld · Oct 07, 2015 at 10:56 AM

Before you use the agent.SetDestination (POS) method, you may call the agent.ResetPath method, because I also encountered this problem, but I solved the problem.i'm chinese

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
2

Answer by mikawendt · Aug 21, 2020 at 01:22 PM

The reason for this issue is that pathfinding can be a tedious process, so to avoid lag the unity path system is using multithreading, to expand the calculation over multiple frames, and take advantage of the multiple cpu-cores even smartphones have nowadays. The issue now is that if you put agent.remainingDistance() in the update method, it will return 0 or the value prior to the agent.destination() call. To avoid this, start the method with

 if(agent.pathPending)
       return;

Then the call will be ignored until the path is calculated.

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
0

Answer by T4NK32 · Aug 11, 2019 at 11:53 AM

It does not behave correctly. And ResetPath doesn't solve the problem (for me).

Waiting a couple of frames (1 is not enough) after setting a new destination works.

The problem shows up in the tutorial "John Lemon's Haunted Jaunt: 3D Beginner":

The ghosts will keep turning around bacause remainingDistance < stoppingDistance.

Fix for "WaypointPatrol.cs":

 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine;
 using UnityEngine.AI;
 
 public class WaypointPatrol : MonoBehaviour
 {
 
     public NavMeshAgent navMeshAgent;
     public Transform[] waypoints;
     public bool LogTurns = false;
 
     int m_CurrentWaypointIndex = 0;
 
     int wait=0, waitTix=3;
 
     void Start()
     {
         navMeshAgent.SetDestination( waypoints[m_CurrentWaypointIndex].position );
         wait = waitTix;
     }
 
     void Update()
     {
         if (--wait > 0) return;
 
         if (navMeshAgent.remainingDistance < navMeshAgent.stoppingDistance)
         {
             m_CurrentWaypointIndex = (m_CurrentWaypointIndex + 1) % waypoints.Length;
 
             if (LogTurns)
                 Debug.Log(this.gameObject.name + " new destination Index: "+m_CurrentWaypointIndex);
 
             navMeshAgent.SetDestination( waypoints[m_CurrentWaypointIndex].position );
             wait = waitTix;
         }
     }
 }
 

As a complete noob this took a while to figure out - but I learned a lot : )

BTW: I'm using the 2019.2.0f1

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

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

23 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

Related Questions

NavMeshPath.corners.length is always 0 1 Answer

NavMeshAgent Destination Problem 0 Answers

All my NavMeshAgents are tilted 1 Answer

NavAgent not following on rebuilt NavMesh 0 Answers

Navmesh agent forward movement 1 Answer


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