Enemy AI Getting Stuck In Behavior Tree


This week I tracked down a bug that caused enemies to only attack the player once and then idle indefinitely, unless the player briefly left their line of sight. Once they reacquired the player, they’d attack again, but as long as the player stayed in view after the first attack, no further attacks would trigger, even after the cooldown expired. Obviously, this made enemies far less threatening than intended.

After much debugging, I found the problem was in the Attack node of our behavior tree. It starts an attack by calling EnemyAttackBegin (via EnemyInterface) and stays InProgress until it gets a completion signal through the FAIMessage system. In BaseEnemy, EnemyAttackBegin checks if the enemy is currently attacking. If not, it starts the cooldown, triggers the attack animation via the AnimInstance, which fires an event when the animation completes. That event signals the Attack node to Succeed, allowing the enemy to continue pursuing the player during cooldown.

The issue was that if the player was still in view when the message was received, the node would immediately call EnemyAttackBegin again. If  the attack was still on cooldown, the !IsAttacking check failed—no animation, no completion event, and the node got stuck waiting. To fix this I added a GetIsAttacking function to EnemyInterface so the Attack node can verify that the attack is ready before calling EnemyAttackBegin. Now enemies properly chain attacks when the player remains in sight.

Leave a comment

Log in with itch.io to leave a comment.