Enemy Attack System using a State Machine in Unity

Matthew Clark
Nerd For Tech
Published in
3 min readOct 13, 2021

--

I have been working on a third-person zombie survival game. I have added the enemy health system so now I will be adding the enemy's attack system.

The first step is to add another collider to the enemy. This collider will be in front of the enemy and will act as the hit range for the enemy attack.

In the enemy script, you will need to add a couple of variables. The first thing to add is the enum to create a state machine for the enemy's behavior. You will need a variable for the IDamagable interface for the player. The last variables to add are the attack delay and the next attack variables to add time between the attacks of the enemy.

Make sure to grab the reference to the player’s IDamageable interface in the start method.

Using the OnTrigger methods you can determine the state the enemy should be in. When the player enters the collider the enemy state should switch to attack and when the player leaves the collider the enemy state should switch…

--

--