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

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.

Or you can use Vector3.Distance:`float distance = Vector3.Distnace(player.transform.position,target.transform.position);`

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

Yes it is possible. Follow along the following :

- Set destination of your agent
- Use http://docs.unity3d.com/ScriptReference/NavMesh.CalculatePath.html to see if path exists.
- Use http://docs.unity3d.com/ScriptReference/NavMeshPath-status.html . It will tell you if you have a complete path or not.
- If your NavMeshPathStatus is “PathComplete”, Unity - Scripting API: AI.NavMeshAgent.remainingDistance 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!

```
public static bool GetPath( NavMeshPath path, Vector3 fromPos, Vector3 toPos, int passableMask )
{
path.ClearCorners();
if ( NavMesh.CalculatePath( fromPos, toPos, passableMask, path ) == false )
return false;
return true;
}
public static float GetPathLength( NavMeshPath path )
{
float lng = 0.0f;
if (( path.status != NavMeshPathStatus.PathInvalid ) && ( path.corners.SafeLength() > 1 ))
{
for ( int i = 1; i < path.corners.Length; ++i )
{
lng += Vector3.Distance( path.corners[i-1], path.corners[i] );
}
}
return lng;
}
```

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

This is also great :).

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

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.