Hi. I just don’t get it. All posts I’ve found mostly says theirs CalculatePath always finds a path even if it shouldn’t (usually the answer is it found a partial path). In my case, no matter what I do, NavMesh.CalculatePath() or NavMeshAgent.CalculatePath() always fails. If I use NavMeshAgent.SetDestination() it works and finds a partial path.
if you only want to calculate the path total travel distance, you don’t need NavMeshAgent component.
and… I’m not sure why you can’t get the result on 2nd script.
it’s too many possibilities
may be you can try to run the following script, hope you can find the answer by yourself.
PS: assign point A & B, near your navmesh. and toggle the script’s enable/disable.
that will draw the result on screen for 10 sec.
using UnityEngine;
using UnityEngine.AI;
public class NavMeshTest : MonoBehaviour
{
public Transform PointA = null;
public Transform PointB = null;
private void OnEnable()
{
if (PointA == null || PointB == null)
return;
NavMesh.SamplePosition(PointA.position, out NavMeshHit hitA, 10f, NavMesh.AllAreas);
NavMesh.SamplePosition(PointB.position, out NavMeshHit hitB, 10f, NavMesh.AllAreas);
NavMeshPath path = new NavMeshPath();
if (NavMesh.CalculatePath(hitA.position, hitB.position, NavMesh.AllAreas, path))
{
Debug.DrawLine(hitA.position + Vector3.up, hitB.position + Vector3.up, Color.red, 10f, true); // a red line float in air
int cnt = path.corners.Length;
float distance = 0f;
for (int i =0; i<cnt - 1; i++)
{
distance += (path.corners[i] - path.corners[i + 1]).magnitude;
Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.green, 10f, true);
}
Debug.Log($"Total distance {distance:F2}");
}
else
{
Debug.LogError("Mission Fail");
}
}
}
I like to add my issue with this. Based on the manual its declared like this:
Code (CSharp):
public bool CalculatePath(Vector3 targetPosition, AI.NavMeshPath path);
So you can include this directly in your if condition.
Now my problem is this returns false for some agents (but not for all) even if there is clearly a path given.
i. e. I make the agent run to target position (player) than return to start position, on a specific trigger it should return to the player. When the trigger hits I check for the path and CalculatePath returns false despite the player not moving.
The agents differ in size, and speed and use different NavMeshes for that reason. But since the problem agent can manoever the whole distance before it returns it can’t be an issue with the Mesh.
var path = new NavMeshPath();
var pathSuccess = _agent.CalculatePath(pos, path); // false
var destSuccess = _agent.SetDestination(pos); // true
I’ve searched the forums & internet the whole afternoon, to no avail. Only thing I found is people with the very same problem and without any reply from Unity officials. Sad…
SetDestination returns whether path was successfully requested, which means agent doesn’t know yet if there’s actually a path there, it just can map your point to some nearest point on navmesh. Contrary to that, CalculatePath does all of pathfinding and returns if it could find path at all