[SOLVED] navMeshPath.status always returning complete

I’m following the answer to this answer here and I’m encountering a problem. No matter what, even if it’s impossible for a path to be completed, my code is always returning true.

    bool CalculateNewPath()
    {
        navMeshAgent.CalculatePath(navigateTo, navMeshPath);

        if (navMeshPath.status != NavMeshPathStatus.PathComplete)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

By all accounts, this should be returning false when I click an area off the navmesh or on a part of the mesh that can’t be traversed to, but it always returns true. What am I doing wrong here?

So it turns out the problem was actually gasp a bug with how Unity was presenting my navmesh, and the code works near perfectly. HOWEVER, I did need to tweak my code a bit to make it account for static objects that weren’t a part of my navmesh. Here’s what the new system looks like:

    public bool CalculateNewPath()
    {
        bool navMeshPathStatus = false;
        bool navMeshSamplePosition = false;

        navMeshAgent.CalculatePath(navigateTo, navMeshPath);

        if (navMeshPath.status != NavMeshPathStatus.PathComplete)
        {
            navMeshPathStatus = false;
        }
        else
        {
            navMeshPathStatus = true;
        }

        NavMeshHit hit;
        if (NavMesh.SamplePosition(navigateTo, out hit, 0.25f, NavMesh.AllAreas))
        {
            navMeshSamplePosition = true;
        }
        else
        {
            navMeshSamplePosition = false;
        }

        if (!navMeshPathStatus || !navMeshSamplePosition)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

As you can see, I now use NavMesh.SamplePosition to determine if an object is actually a valid point on the navmesh. This has a few caveats for use cases outside of what I’m doing (for instance, it doesn’t handle edges super well) but for the purposes of my RTS it works fine.

2 Likes

Thanks! Just what i needed.

But there is a problem! SamplePosition only checks, if the destination point, is a valid point on the navmesh, not if an object is able to reach it. If you have a terrain with unreachable hight platforms, then those platforms are also a valid point on the mesh (if the area is baked blue), but if isolated, there is no way, a valid path can be calculated.