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.




A passionate gamer whose goal is to work in video game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrating Robot Framework with Browserstack…

How to create a repository using git and GitHub for beginners.

The hidden value of your server logs

Wazirx Crypto Price Scrapper | Python & Data Analytics — Part II

Difference between a UI developer and a front-end developer

Physics in Unity!

Debugging GKE unprivileged containers with gdb and nsenter

Switching from Rails to Node: A Comparison

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

2.5D Platformer: Moving Platforms

Managers, Main Menu and Loading screen

Cleaning up the Homing Missile code

Different Lights in Unity.