How to detect when there is no possible path?

Hi, I am building a turn-based combat system, and so far, it works as intended until there is no possible path between objects. I guess what happens is A* is trying to find the most “okay” path possible, which I do not want. For example, in the picture, A* calculates a path from green to red with 2 counts.

And here finds a path from red to green with 7 counts.

So how can I detect when there is no path?

Here is my path find method in case it is needed.

int UpdatePath(GameObject pathTarget)
    {

        Path p = GetComponent<Seeker>().StartPath(transform.position, pathTarget.transform.position, OnPathComplete);
        AstarPath.BlockUntilCalculated(p);
        Debug.Log("Path Found! Path Count is " + path.vectorPath.Count);
        path = p;
        int count = path.vectorPath.Count;
        return count;

    }

    void OnPathComplete(Path p)
    {
        if (!p.error)
        {
            currentWaypoint = 0;

            path = p;

        }
        else Debug.Log("error");

    }

Thanks in advance for the help.

Hi

There are multiple method depending on what you want:

The easiest is probably to decrease the A* Inspector -> Settings -> Max Nearest Node Distance. That will make the algorithm search a smaller distance when trying to find a valid node to walk to.

An alternative is to use the PathUtilities.IsPathPossible method. See https://arongranberg.com/astar/docs/pathutilities.html#IsPathPossible for more info.