Why does NavMesh.CalculatePath() fail while NavMeshAgent.SetDestination() works?

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.

This one works:

private void Start()
    {
        agent = gameObject.AddComponent<NavMeshAgent>();
        agent.speed = 3.5f;
        agent.radius = 1.69f;
        agent.height = 2.57f;
        if(!agent.SetDestination(Base.First().transform.position))
            Debug.Log("Failed");
        agent.isStopped = true;
    }

And this one doesn’t:

private void Start()
    {
        agent = gameObject.AddComponent<NavMeshAgent>();
        agent.speed = 3.5f;
        agent.radius = 1.69f;
        agent.height = 2.57f;
        path = new NavMeshPath();
        NavMesh.CalculatePath(transform.position, Base.First().transform.position, NavMesh.AllAreas, path);
        if (path.status == NavMeshPathStatus.PathPartial)
        {
            Debug.Log("Is partial, yey"); // <= never happens
        }

        agent.SetPath(path);
        agent.isStopped = true;
    }

The second one doesn’t even have any corners in path.corners. How is that possible?

The thing is, I only need the path to check how long it is. That’s why I need it to calculate immediately.

Why would you expect only NavMeshPathStatus.PathPartial , what about NavMeshPathStatus.PathComplete?

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");
        }
    }
}

Well, It could be != PathInvalid, but I’m sure the only path to that object that can be created is partial.

Thanks for replay, I’ll check that probably tommorow.

Guys,

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.

Any insight is welcome.

Same issue here:

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

1 Like