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

Delete All Git Branch Except 1 With Bash Script

Multiple Gravium masternodes on single VPS

IAAS, PAAS, SAAS… What’s Next ?

How To Use JMeter for PostgreSQL Database Performance Load Testing

Behind our maps: MapZen Tangram

Writing your first Java program

Enterprise Scale Azure Landing Zones via Azure DEVOps using Terraform and CAF Modules: Rover

READ/DOWNLOAD] Cissp Official (Isc)2 Practice Test

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

Building A Stealth Game — Helping Your Guards “See”

Gamedev Log: “Slash’n’crack” #4 (Unity/C#)

How I used scriptable objects to build a powerup system

Creating Elevator in Unity — Part 3: Moving Elevator.