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!
     
    ben-rasooli likes this.
  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 :).