Stop agent from entering same node as other agent

Hi there,

I am using a GridGraph with Unity in 2D mode. I am using this script ( that Aron created for those of us using 2D, and it works great for pathfinding. I am having trouble figuring out a few issues:

Right now I have 2 agents that are moving towards the same target. As an agent enters a new node, I set the penalty of this node to a high value (10000) so that the other agent avoids this agent when pathfinding. This doesn’t seem to work too well, as they still enter the same node at times when moving around.

When an agent reaches its target, I set the node to Walkable = false and run;. This should make it so the other agent can’t enter this node on it’s way to the target, but it still ends up on this exact same node even though it’s unwalkable. Is there a better way to prevent agents from being on the same tile as another agent?


What you really seem to be looking for is cooperative pathfinding:
That is still experimental however, so it’s not a solution right now.

Are your agents recalculating their paths often enough? If they enter an unwalkable node that would likely be because they haven’t recalculated their path since the node was made unwalkable.

I set the repath rate to 0.25 and changed the end point on the seeker to “Snap to node”. It seems to be working properly now.

One more question, it seems as though my agent is not moving to the nearest position between itself and a target on an unwalkable node. See the attached image for a better understanding of what’s happening.

The heart is my agent, which is moving from the brownish colored block to the brick. For some reason, it keeps wanting to travel to the far side of the brick rather than the side closest to it.

Tried the code you posted in another thread, but it did not work:

Node nearestToSoldier =, NNConstraint.Default).node;

//This NNConstraint will constrain the search to the closest walkable node
//which has the same area id as the nearestToSoldier node
NNConstraint nn = NNConstraint.Default;
nn.constrainArea = true;
nn.area = nearestToSoldier.area;
Node nearestToBlock = (Node), nn);