Behavior State- Game Dev Series 131
Objective: creating enemy actions state by using enum.
To simulate a realistic behavior of movement, we usually need to create at least 2 action state of character: idling and walking. And it would be a good chance to use enum.
In this article, we would create action state by using enum.
Create Enum state
Let’s assume that you already have a functional enemy would chasing and attacking Player.
In my script of enemy, I use trigger of enemy to detect if Player is close enough to attack, and chasing Player if it is out of range.
First, we need to create a public enum to declare the name of state.
Then we also need a variable to set the current state.
Now we can make the enemy doing actions depend on the state by using switch statement in Update().
Switching between chasing and attacking
With this switch, we can make enemy chasing Player when it is in Chase state.
And let’s switch the state into Attack when Player is in a specific range.
In here, I would use trigger to change the state.
Now we have change the state when reaching Player, we can apply the attack method to Attack state.
We have successfully create the switching between Chase and Attack.
The next article would create the Idle state and make Enemy would detect Player and change state.