IsPathPossible always returning true

Hi, I’m trying to program different behaviour for my AI based on whether it can reach its target. I read that the following code


        GraphNode node1 = AstarPath.active.GetNearest(enemy.transform.position, NNConstraint.None).node;
        GraphNode node2 = AstarPath.active.GetNearest(targetPosition, NNConstraint.None).node;

        if (PathUtilities.IsPathPossible(node1, node2))
        {
            if (_debug) Debug.Log($"Found path from {enemy} to {targetPosition} ({target.ToString()})");
            return true;
        }
        else
        {
            if (_debug) Debug.Log($"Could not find path from {enemy} to {targetPosition} ({target.ToString()})");
            return false;
        }

would work, however it continually returns true, even if my AI can’t reach the player. When I’m unreachable, the AI keeps moving against the border of the navmesh towards the player, unable to proceed any further, but the code above still returns true. I’ve tried NNConstraint.Default and .None, but neither make a difference.

Just a thought but do you have more than one graph? Are those nodes on the same graph?

Good hunch, but nope, just one graph. I also read about decreasing the “Max Nearest Node Distance,” so I did and I got strange results – IsPathPossible continued to return true, but I also began getting “Path Failed,” so it appears something isn’t lining up properly…

Do you have a screenshot of the nav mesh etc?

hey David, here’s a photo of my current setup: https://imgur.com/a/uttVRug

Hi

It looks like the player is completely outside the navmesh.

What you do in your code is to find the closest point on the navmesh to the player and check if that node can be reached. That will return true even if the player is outside the navmesh since the closest point on the navmesh is still reachable.

I would recommend doing a check to see if the player is far away from the navmesh.

var nearest = AstarPath.active.GetNearest(targetPosition, NNConstraint.None);
if (Vector3.Distance(targetPosition, nearest.position) > someThreshold) {
    // Player is far away from the navmesh, probably not reachable
}
1 Like

Ah that did it! And glad to know there’s an option for checking whether the closest point is traversable. Thank you :slight_smile:

1 Like