// Unity's example function..
public float CalculatePathLength(NavMeshAgent nav, Vector3 targetPosition)
{
// Added condition since it was returning impossible values if given same position :P
if (nav.transform.position == targetPosition)
return 0;
// Create a path and set it based on a target position.
var path = new NavMeshPath();
if (nav.enabled)
nav.CalculatePath(targetPosition, path);
// Create an array of points which is the length of the number of corners in the path + 2.
var allWayPoints = new Vector3[path.corners.Length + 2];
// The first point is the player position.
allWayPoints[0] = player.transform.position;
// The last point is the enemy position
allWayPoints[allWayPoints.Length - 1] = targetPosition;
// The points inbetween are the corners of the path.
for (var i = 0; i < path.corners.Length; i++)
allWayPoints[i + 1] = path.corners[i];
// Create a float to store the path length that is by default 0.
float pathLength = 0;
// Increment the path length by an amount equal to the distance between each waypoint and the next.
for (int i = 0; i < allWayPoints.Length - 1; i++)
{
pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
}
return pathLength;
}