- Home /
Why are NavMeshPath.corners all 0, 0, 0 when generated with NavMesh.CalculatePath?
I'm currently creating a function which, when called, calculates the distance to each vector in an array of waypoints and returns the three closest way points.
This is for the Unity Stealth Project.
I thought I had it working but then realised my function always returned the same three waypoints, but occasionally in different orders. The issue I found (by using Debug.Log to show the path corners of each NavMeshPath the function generated) was that all the points in the NavMeshPath were 0, 0, 0.
When I've previously used NavMeshAgent.CalculatePath, I didn't have this issue, in fact I'm successfully using it to make my AI return to their closest patrol point (as opposed to the last patrol waypoint).
Here's the code I'm using:
int FindNearestWaypoints (Vector3 sightingPosition, Transform [] WayPoints)
{
int minDistanceIndex;
float[] distanceToWaypoint = new float [WayPoints.Length];
// An array of floats to store the distances to each of the AI's patrol waypoints.
NavMeshPath [] returnPaths = new NavMeshPath[WayPoints.Length];
// An array of NavMeshPaths, the lengths of which we want to measure.
for (int i = 0; i < distanceToWaypoint.Length; i++)
{
returnPaths [i] = new NavMeshPath();
NavMesh.CalculatePath (sightingPosition, WayPoints[i].position, NavMesh.AllAreas, returnPaths[i]);
Vector3 [] allPoints = new Vector3[returnPaths[i].corners.Length+2];
allPoints [0] = sightingPosition;
allPoints [allPoints.Length-1] = WayPoints[i].position;
for (int j = 0; j < returnPaths[i].corners.Length - 1; j++)
{
allPoints[j+1] = returnPaths[i].corners[j];
}
// Adds the path corners to the array of Vector3s.
for (int k = 0; k < allPoints.Length - 1; k++)
{
distanceToWaypoint [i] += Vector3.Distance (allPoints[k+1],allPoints [k]);
}//Calculates the distance between each point in the Vector3 array and sums it.
}
minDistanceIndex = FindMininumIndex (distanceToWaypoint);
return minDistanceIndex;
}
When I used Debug.Log, I found that all the allpoints values were actually 0, 0, 0.
And yes this is a modified version of the hearing distance script from the tutorial.