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
5
Question by VamppiV · Jun 21, 2014 at 03:30 PM · navmeshnavmeshagent

nav remaining distance wrong

I have problem with nav remaining distance. I have code like this:

 void Attack(){
             nav.SetDestination (player.transform.position);
         Debug.Log (nav.remainingDistance);
             if (nav.remainingDistance<0.4f) {
             gameController.LoseGame ();
                 }

I'm trying to see what's wrong by debug.log. Sometimes everything work. But sometimes nav.remainingDistance is about 0.0.. and after second it gets good value. My Enemy always start at the same place - my player too. Why it sometimes works, sometimes not? I was trying everything and I don't know where is the problem.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by kubajs · Mar 17, 2017 at 10:00 PM

In case you use coroutine, just put this somewhere to the top:

 if (_agent.pathPending)
     yield return null;

Example of my simple coroutine:

 private IEnumerator Patrol()
     {
         while (_patrolling)
         {
             if (_agent.pathPending)
                 yield return null;
 
             if (_agent.remainingDistance < _agent.stoppingDistance)
             {
                 SetNextWaypointAsTarget();
             }
 
             yield return null;
         }
     }

 








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
5

Answer by D3Duck · Jan 10, 2016 at 12:15 PM

For anyone seeing this, or in case you still have this question.

When SetDestination() is called, I found that remainingDuistance is NOT immediately updated. It takes a frame (maybe one Update() call?) to set this.

So the solution is to wait one frame with a Coroutine or break out of the Update() function or whatever else. Point being, one frame later the remainingDistance will be correct.

Comment
Add comment · Show 1 · 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 Ziplock9000 · Nov 01, 2018 at 04:21 PM 0
Share

While the root cause might be true, your solution is a horrible hack that is not recommended at all.

avatar image
3

Answer by rickygow · Mar 20, 2016 at 04:31 PM

I found in some forum that it's because it's still Finding it's path and doesn't have it's remaining distance yet. so you can use this instead

         //-- If navMeshAgent is still looking for a path then use line test
         if(navMeshAgent.pathPending)
         {
             dist = Vector3.Distance(transform.position, moveTarget.position);
         }
         else
         {
             dist = navMeshAgent.remainingDistance;
         }


but still a little messy with the pathPending, me just use the Vector3.Distance as every movement now, less buggy for my Classic Moba click-to-move.

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 kelvin_feng · Apr 26, 2018 at 02:44 AM

While the player is stopped, the remainingdistance equals zero even you set the destination immediately. After you set the destination, agent would set the pathPending true and spend times on find a path. So the remainingdistance would keep the old value at least one frame.You can have a try with blow code.

 if(!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
 {
     gameController.LoseGame();
 }

Cheers

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
1

Answer by LightclawJL · May 26, 2017 at 05:13 AM

If anyone needs to get the remaining distance for something like geting the closest objective, you can use a code like this one:

     public Dictionary<string,float> getObjectsDist(){
         Dictionary<string, float> dic = new Dictionary<string, float>();
         NavMeshAgent agent = GetComponent<NavMeshAgent>();
         NavMeshPath path = new NavMeshPath();
         foreach (string key in lib.Way.Keys) //this was the dictionary with my targets transforms.
         {
             Transform trans = lib.Way[key];
             NavMesh.CalculatePath(transform.position, trans.position, agent.areaMask, path);
             float dist = 0f;
             for (int i = 0; i < path.corners.Length - 1; i++)
             {
                 dist += Vector3.Distance(path.corners[i], path.corners[i + 1]);
             }
             dic.Add(key, dist);
         }
         return dic;
     }

This function returns a Dictionary with a name and distance to it.

Basically, you can crate an empty path, use your navmesh to calculate a path between 2 transforms, and the you go corner from coner on it (so you iterate on each line on the final path) and sum all the distances, getting your final distance.

Hope it helps.

*Sorry for bad english...

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

8 People are following this question.

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

Related Questions

Navmesh Agent "move back" when player comes to close? 1 Answer

NavMeshAgent resume original position and facing direction C# 0 Answers

set the angular velocity of nav mesh agent directly? 1 Answer

NavMeshSurface at runtime - agent not on surface? 2 Answers

Procedural NavMesh Instantiating 0 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