Search Unity

unity navmesh get remaining distance (infinity)

Discussion in 'Navigation' started by kumar123k, Jul 14, 2016.

  1. kumar123k

    kumar123k

    Joined:
    Jul 5, 2016
    Posts:
    25
    HI
    i am getting problem with navmesh i am trying to get remaining distance when i get distance some times it is showing infinite distance.
    how to get the distance , i have noticed that get reaming distance function calculate the nearest node instead of the final destination.

    can some one give the solution for this.
     
    Novack likes this.
  2. Bechmann

    Bechmann

    Joined:
    Nov 5, 2017
    Posts:
    27
    Novack likes this.
  3. Cole_Slater

    Cole_Slater

    Joined:
    Sep 25, 2017
    Posts:
    1
    This is a bit old, but I came across this thread and I'm sure other people will too. Here's my solution:
    Code (CSharp):
    1. public float RemainingDistance(Vector3[] points)
    2.     {
    3.         if (points.Length < 2) return 0;
    4.         float distance = 0;
    5.         for (int i = 0; i < points.Length - 1; i++)
    6.             distance += Vector3.Distance(points[i], points[i + 1]);
    7.         return distance;
    8.     }
    Just pass in yourNavMeshAgent.path.corners
     
    Donezo_Lamb and Cassiano14 like this.
  4. unity_QJ7RazXzghZCzA

    unity_QJ7RazXzghZCzA

    Joined:
    Jul 9, 2021
    Posts:
    104
    Why this is still not fixed in Unity?
    Why do we need this useless remainingDistance if its not reliable at all?
    Why this is undocumented?
     
    THE2FUN likes this.
  5. THE2FUN

    THE2FUN

    Joined:
    Aug 25, 2015
    Posts:
    63
    Still same in 2023 (in two months 2024)
     
  6. Ginnjo

    Ginnjo

    Joined:
    Aug 15, 2020
    Posts:
    1
    One of the most common cause is the scale of the object you have NavMeshAgent attached to.

    Workaround for this is a placeholder as parent =>
    1. Create Empty Object and give this object the NavMeshAgent
    2.
    Put the Object/Character as child of that "EmptyObject" and use
    agent = GetComponentInParent<NavMeshAgent>();
    to get agent if you have controller script for the character.
    3. Now scale the mesh fitting size under the "Empty Object" while the parent is scaled 1,1,1 that has the Agent

    4.(bonus)
    Now you can use the Empty Object's position as a "spawn position" where the object started moving and you can return the "enemy" back to 0,0,0 in case of running out of its range of combat.
    Or character's spawn point or check point.​
     
  7. br3nr

    br3nr

    Joined:
    Jan 26, 2024
    Posts:
    1
    This fixed the issue for me. The agent model I was using had been scaled down quite a lot and was causing the remainingDistance function to return zero. Thanks so much :)