NavMesh CalculatePath not working correctly

Using this method to determine if my NavMeshAgent can reach it’s target destination doesn’t correctly work. If the target is unreachable by the agent, whether the target is simple not on the NavMesh or there is an obstacle blocking the path of the agent, CalculatePath always returns true.

Likewise, if I debug log out the NavMeshAgents path.status, if the target is not on the NavMesh at all, as in the GameObject itself is position far away from the test area, it still returns PathComplete.

Not sure what I’m doing wrong here.

using UnityEngine;
using System.Collections;

public class NavMeshController : MonoBehaviour {

    public GameObject target;
    NavMeshAgent agentController;
    bool targetingPlayer = false;

    Vector3 startPosition;
    // Use this for initialization
    void Start () {
        agentController = GetComponent<NavMeshAgent>();
        startPosition = gameObject.transform.position;
    }
   
    // Update is called once per frame
    void Update () {

      
       NavMeshPath path = new NavMeshPath();
       
       Debug.Log(agentController.CalculatePath(target.transform.position, path))
     
        if (agentController.path.status != NavMeshPathStatus.PathPartial)
        {
            agentController.destination = target.transform.position;

        }
        else
        {
            agentController.destination = startPosition;
        }

    }
}

Obviously it’s very basic and just for testing purposes, but I still don’t understand why It’s returning both Path Partial and Path complete when the navMesh agent clearly cannot reach the target destination. As well as why CalculatePath always returns true in the same scenario.

in line 25 i’m guessing you meant to test path.status instead of agentController.path.status ?

On line 25, I’m trying to find if the actual path status of the actual NavMeshAgent attached to the gameObject, not the path object I created on line 21. The line on 21 was an attempt at getting the calculatePath to return false if a path can’t actually be found to the target. Before I changed it to the path game object, I was doing it like this

   Debug.Log(agentController.CalculatePath(target.transform.position, agentController.path))

And that still returned true even if a path wasn’t found.

Figured out what to do.

NavMeshPath path = new NavMeshPath();

agentController.destination = target.transform.position;

agentController.CalculatePath(agentController.destination, path);

if(path.status == NavMeshPathStatus.PathPartial)
{
 //do whatever
}

doing the calculation on the agentController.path doesn’t actually work. So thank you @Jackob_Unity for making me realize that. I feel so stupid aha.

2 Likes

Guys,

I like to add my issue with this. Based on the manual its declared like this:

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.