A* Pathfinding Project

Trouble getting position on Layered Grid Graph


#1

Hi, I’m spawning enemies on my level and I’m trying to get safe positions on the grid. I’m saying safe because the level has some platforms (all platforms are connected) and enemies can only be spawned on those platforms.
I wrote a method to return a safe position but in some cases the position is out of the grid.
I can’t use constraint to the grid because for some reason agent can’t go up slopes…

This is the method I’m using, am I missing anything?

Vector3 GetClosestPositionOnGraph(Vector3 position, int graph)
{
constraint = NNConstraint.None;
constraint.graphMask = 1 << graph;
infoPosition = AstarPath.active.GetNearest(position, constraint);
return infoPosition.position;
}

Thanks!


#2

Hi

If you only want it to return walkable nodes you need to add

´´´
constraint.constrainWalkability = true;
constraint.walkable = true;
´´´


#3

Hi! Thanks so much!! That worked :slight_smile: