How does a game enemy decide what to do? State machines for beginners
Ever wondered how a game guard knows when to chase you? Most enemies run on a state machine: they are always in exactly one state, like patrol, chase or attack, and a few simple rules decide when to switch.
Drag the player around the demo, change the chase and give-up distances, and see why two different distances stop the guard from flickering. Then sketch your own enemy with three states.

You walk into a room and a guard stands still. You step closer and he turns, shouts, and runs at you. You hide and he wanders back to his post. It feels alive, but the code behind it is usually small and simple: a state machine.
One thing at a time
A state machine says an enemy is always in exactly one state, such as Patrol, Chase or Attack. Each state has its own little job. In Patrol the guard walks between two points. In Chase he moves toward you. In Attack he swings his sword.
The enemy does not think about everything at once. It only asks one question: "Is it time to leave this state?" That keeps the code easy to read and easy to fix.
Transitions: the rules for switching
A transition is a rule that moves the enemy from one state to another. Each rule is an "if". If the player is closer than 120 pixels, go from Patrol to Chase. If the player is closer than 30 pixels, go from Chase to Attack. If the player has been out of sight for a few seconds, go back to Patrol.
Notice that the rules are different for entering and leaving a state. That gap is deliberate, and you can try it below.
Why two distances beat one
Try setting both sliders to the same number and hover the player right on the ring edge. The guard flips between Chase and Patrol again and again. This is called flickering, and it is the classic beginner bug. The fix is to make it harder to leave a state than to enter it. Game makers call that gap hysteresis. A short delay before giving up, like the one in the demo, helps too.
What it looks like in code
Most beginner projects write a state machine in a few lines. A variable holds the current state, and each frame the game runs the code for that state and checks its transitions.
if (state == "patrol") {
walk_between_points()
if (distance_to_player < 120) state = "chase"
}
else if (state == "chase") {
move_toward_player()
if (distance_to_player < 30) state = "attack"
if (distance_to_player > 180) state = "patrol"
}
Engines such as Godot and Unity let you build the same idea with nodes, enums or visual graphs, but the logic is the same.
Sketch before you code
Before writing anything, draw circles for states and arrows for transitions on paper. For every circle ask two questions: how do I get in, and how do I get out? A state with no way out is a bug waiting to happen. A state with five exits probably needs splitting in two.
Start with three states. Once Patrol, Chase and Attack feel right, add one more, such as Flee at low health. Each new state should earn its place by changing how the enemy plays.
Where state machines run out
State machines work well for small sets of behaviours. When an enemy has dozens of states the arrows turn into spaghetti, and developers often move to other approaches such as behaviour trees. You do not need those for a first game. Three states and a few clear rules already make an enemy that feels like it is paying attention.
More in Making Games
all Making Games →2 Dec How do games branch a conversation? Dialogue choices, flags and locked answers Dialogue in games is a map: each line of text is a node, and each answer is a road to another node. Add a few flags and numbers the game… Making Games
24 Nov How does a 2D game fake depth? Parallax scrolling and background layers explained Walk through a game level and the faraway mountains barely move while the trees rush past. That trick is parallax scrolling, and it makes a… Making Games
16 Nov Why do menus and movements feel smooth? Easing and tweening explained Press a button in a game menu and the panel slides in, slows down and settles. That feel comes from easing: changing the speed of a… Making Games
3 Nov How do games make fire, smoke and sparks? Particle systems explained Fire, smoke and sparks in games are usually not drawings or videos. They are swarms of tiny short-lived particles made by an emitter. This… Making Games
1 Nov How does an enemy find its way around a wall? Pathfinding and A* explained How does an enemy find its way around a wall? Walking straight at you works only until the first obstacle. Pathfinding treats the map as a… Making Games
30 Oct How do game mods work? Load order and why the last mod wins Ever installed two mods and watched one quietly stop working? A mod is a small package that swaps or adds the data files a game reads, and…