Targetting Issues

Hi, I’m currently working on a resource gathering ai but am running in to certain issues. How do I ensure that the AI doesnt try to path to either of the red squares (inaccessible and accessible only through a different area), while allowing them to target blocks like the green square which are highlighed as impassable but accessible from the current area?

Hi

Sorry for the late reply.
So you have a list of targets and you want to check which ones of them are reachable, except the targets themselves are not actually accessible, only the tiles next to them?

You can do something like this:

var playerNode = AstarPath.active.GetNearest(player.position, NNConstraint.Default).node;

var node = AstarPath.active.GetNearest(someResource.position, NNConstraint.None).node as GridNodeBase;
var graph = node.Graph as GridGraph;
// Check all neighbours of the node (and the node itself)
for (int dx = -1; dx <= 1; dx++) {
	for (int dz = -1; dz <= 1; dz++) {
		var x = node.XCoordinateInGrid + dx;
		var z = node.ZCoordinateInGrid + dz;
		// Check if the new coordinate is inside the grid, and if the player can reach that node
		if (x >= 0 && z >= 0 && x < graph.width && z < graph.depth && PathUtilities.IsPathPossible(playerNode, graph.GetNode(x,z))) {
			// Yay, there is a path from the player to a node next to this one
		}
	}
}
1 Like