How do I stop the AI from pathfinding to the target on a different floor?

So I have noticed that if the target is inaccessible on another floor, it will move to the X/Z position of the target, but on the wrong floor.

Example:
A building with 2 floors.
The target is on floor 2, but is unreachable.
So the Character moves to the X/Z position of the target but on floor 1.

I am using the Layered Grid Graph.

How can I stop them please? I would rather they not move at all. Does it call something when the path cannot be found that I can intercept perhaps?

Thanks :slight_smile:

Hi

You can reduce the A* Inspector -> Settings -> Max Nearest Node Distance to make the path fail if the closest point on the navmesh is too far away from the destination.

Hi thanks for the reply. The problem with that method is I want them to be able to reach far destinations if the path is clear. I want if they cant reach the destination for them to never move at all.
Thanks.

Hi

You can check if there is a path to the destination using IsPathPossible.
See https://arongranberg.com/astar/docs/pathutilities.html#IsPathPossible

I’ve added that now, thanks for that. I still get the original issue though. It seems they consider another floor for the target as a valid path. I’m assigning the target destination to the AIDestinationSetter script.

So I need a way that allows people to move to targets on other floors, but not move to floors that the target isnt actually on.
Is it possible to get the position of the node it is targetting and compare that to the target on the Y coordinate? That way I can check if it is targetting another floor and block it from doing so.

Thanks :slight_smile:

I still cannot find this information :confused: Any help is really appreciated.

Hi

If it’s of a different floor that the agent cannot actually reach then IsPathPossible should be returning false. Make sure you do not assign the destination in that case. You can debug this by setting A* Inspector -> Settings -> Graph Coloring to ‘Areas’. Each differently colored area is considered not reachable from any other the other regions (and vice versa).

You can also get the closest point on the navmesh using:

var closest = AstarPath.active.GetNearest(destination, NNConstraint.Default).position;

Hi thanks for getting back to me.

I cant find the A* Inspector, is that a tool?
I tried this:
Vector3 closest = AstarPath.active.GetNearest(TargettingScript.target.position, NNConstraint.Default).position;
Debug.Log("Closest Node: " + closest);
if (!PathUtilities.IsPathPossible(node1, node2) || TargettingScript.target.position.y != closest.y)
{
TargettingScript.target = null;
}

But they still move to the incorrect floor. Is setting to null not enough to get them to stop moving? I want them to be like - I cant reach the actual target on the correct floor, so I will not move.

Ah nevermind that was my mistake, that is working.
Thank you!

1 Like