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.
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?
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
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 : )