Having issues with controlling AI behavior when a path is not available

I’m making a top down shooter with some tower defense elements, and I need to be able to punish the player for blocking off parts of the map so that enemies cannot reach him/her. I added the following to the AIPath script’s update. tarTrans is the AI unit’s target, which is usually the player. isBlocked is a bool that starts as false and is set to true when the unit realizes that it is blocked. Once isBlocked is set to true, a message is sent telling the AI unit to enrage. This gives the unit the ability to destroy player placed objects in order to reach its target.

`if (tarTrans != null && !isBlocked)
{
Node node1 = AstarPath.active.GetNearest(tr.position).node;
Node node2 = AstarPath.active.GetNearest(tarTrans.position).node;

bool isPathPossible = PathUtilities.IsPathPossible(node1, node2);
				
if (!isPathPossible)
{
	isBlocked = true;
	SendMessage("InfBlockedEnrage");
}

}`

The main problem that I am having is that units will sometimes enrage when they shouldn’t.

One of my enemy types will charge in a straight line at a player when it is close. If the enemy charges and collides with a wall, it will often think that it is blocked upon calculating its next path, even though it is not.

Another cause of this bug is when the player is hugging obstacles, particularly rounding corners while hugging them, the enemy will think that the player is unreachable and enrage.

How can I clean up this behavior so that the enemy only enrages as a last resort when it is 100% blocked off from its target?

The issue is that your calling CheckIfPathIsPossible more often then needed~ and from positions that are not needed~

Try this:

Whenever the player places a building, check for if it blocks the path:

The Start Node should be the units spawn, and the *End Node should be the TD player’s thing there trying to protect~

`public bool CheckIfPathIsPossible(Vector3 PathStart,Vector3 PathEnd){

	Pathfinding.Node node1 = AstarPath.active.GetNearest(PathStart, NNConstraint.Default).node;
	Pathfinding.Node node2 = AstarPath.active.GetNearest(PathEnd, NNConstraint.Default).node;
	
	if (!Pathfinding.PathUtilities.IsPathPossible (node1,node2)) {	
		
		return(false);
	}
	else{
		
		return(true);
	}
}`

Also, a related method which might be of interest is:

http://arongranberg.com/astar/docs/class_pathfinding_1_1_graph_update_utilities.php#a5cca2a0658c53d66d366a2ba495498f1

Alright, I see. I had the check running in update because I wanted the enrage to occur in the least punishing way possible so that only 1 or 2 enemies would enrage upon reaching the end of the possible path to the player, instead of a wave of 20 enemies all enraging at spawn. The latter would be almost certainly game ending, but the prior would be a way of letting the player know that their building placement wasn’t acceptable without straight up ending the game.

Looking at the code and link you guys provided is giving me a better idea of how to go about it though.

PS… this is offtopic but huge thanks for providing such a well maintained/documented AI system for free :smiley:

Also, the solution that burdock proposed can be used for each unit usually as well.
The key is NNConstraint.Default. That will make it return the closest walkable node, so it will not by mistake return an unwalkable node when moving close to a tower for example.

PS: Yeah, I want to support the indie game developers which sometimes have no budget at all. If you end up making a successful game, consider purchasing the pro version to support the continued development of this project : )

If you need some ideas, I can write a short script to do something close to what you want!!