Search Unity

Getting the distance in nav mesh

Discussion in 'Scripting' started by Yash987654321, Apr 2, 2015.

  1. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    is it possible to get the distance between target and object of shortest part of nav mesh. I am using Simple A* but i want to know about both
     
  2. mcapousek

    mcapousek

    Joined:
    Jan 11, 2013
    Posts:
    9
    It can be done by using navigation-agent to compute path from 'A' to 'B' (resulting path will be sequence of points on nav-mesh) and then just simply "summming" distances between those points.
     
  3. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Or you can use Vector3.Distance:
    Code (csharp):
    1. float distance = Vector3.Distnace(player.transform.position,target.transform.position);
     
  4. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    sorry i should have said length of the path between the target and ai (my english is really bad) Vector3.distance just gives the distance between to object so if it is not useful to nav mesh :(
     
  5. farhanblu

    farhanblu

    Joined:
    Dec 29, 2014
    Posts:
    49
    Yes it is possible. Follow along the following :
    1. Set destination of your agent
    2. Use http://docs.unity3d.com/ScriptReference/NavMesh.CalculatePath.html to see if path exists.
    3. Use http://docs.unity3d.com/ScriptReference/NavMeshPath-status.html . It will tell you if you have a complete path or not.
    4. If your NavMeshPathStatus is "PathComplete", http://docs.unity3d.com/ScriptReference/NavMeshAgent-remainingDistance.html will give you the distance

    I have experienced that if at any point your agent seems to be going further away from your destination at start (due to rerouting of path), you will get remainingDistance to be 'infinity' until it actually starts moving closer to the destination. You might want to experiment with it youself!
     
  6. mcapousek

    mcapousek

    Joined:
    Jan 11, 2013
    Posts:
    9
    Code (CSharp):
    1.  
    2.     public static bool GetPath( NavMeshPath path, Vector3 fromPos, Vector3 toPos, int passableMask )
    3.     {
    4.         path.ClearCorners();
    5.        
    6.         if ( NavMesh.CalculatePath( fromPos, toPos, passableMask, path ) == false )
    7.             return false;
    8.        
    9.         return true;
    10.     }
    11.        
    12.     public static float GetPathLength( NavMeshPath path )
    13.     {
    14.         float lng = 0.0f;
    15.        
    16.         if (( path.status != NavMeshPathStatus.PathInvalid ) && ( path.corners.SafeLength() > 1 ))
    17.         {
    18.             for ( int i = 1; i < path.corners.Length; ++i )
    19.             {
    20.                 lng += Vector3.Distance( path.corners[i-1], path.corners[i] );
    21.             }
    22.         }
    23.        
    24.         return lng;
    25.     }
     
  7. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Thanks for this it is what i wanted. Its awesome for unitys nav mesh... but is it possible in Simple A*'s grid based solution as I am using that because it suits me and because i generate level at awake from two scripts
     
  8. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    This is also great :).
     
  9. Lee_Kaili

    Lee_Kaili

    Joined:
    Sep 24, 2019
    Posts:
    3
    Hi, may i know how the formula of calculating NavMeshAgent.remainingDistance?
    (The distance between the agent's position and the destination on the current path.)
    is it using d=√((x_2-x_1)²+(y_2-y_1)²) this formula ?

    Thank you
     
    Last edited: Mar 19, 2021
  10. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    That would correct for getting the length (magnitude) of a 2D vector, yes. It's your basic Pythagorean theorem - A^2 + B^2 = C^2

    However, if you mean the length along the path, then assuming your agent is at one end of a path, and your destination is at the other (why wouldn't it ever be?), then you can refer to lines 16 through 24 of the code posted by mcapousek above:
    And just for clarity - you don't need to actually write out the math to get the distance - you can just use Vector3.Distance, or Vector2.Distance if you want a "bird's-eye" distance.

    Also, if you ever want to just compare two distances to see which is greater, you should use .SqrDistance instead, as it's the same formula but without the expensive square root - and the bigger vector will always be bigger whether you get the roots of them or not.