NPC Movement Patterns
How do game characters know where to go? Discover the simple rules behind smart-looking AI.
How do game characters know where to go? Discover the simple rules behind smart-looking AI.
The simplest NPC behavior: walk between a set of waypoints, over and over.
A list of (x, y) positions the NPC visits in order. When it reaches the last one, it goes back to the first.
Check if NPC is "close enough" to the waypoint (within a few pixels), then switch to the next one.
Guards in stealth games like Metal Gear Solid use patrol patterns. Speedrunners memorize these exact paths to slip past undetected!
Move your mouse (or tap) to control the player. Watch the enemy chase you!
Subtract positions (target - self) to get a vector pointing toward the target.
Divide by distance to get a unit vector (length 1), then multiply by speed for consistent movement.
Pure chase AI is predictable! Games like Pac-Man give each ghost different behaviors - one chases directly, one predicts where you're going, and one acts randomly.
The opposite of chase - the NPC runs away from you! Move toward it to scare it off.
Flee uses the same math as chase, but subtract player from NPC instead of NPC from player.
NPCs only flee when the player is within a certain distance. Otherwise they stay calm.
In Minecraft, passive animals flee when attacked. The flee direction is randomized slightly so they don't all run in a straight line!
Random movement that looks natural. Great for idle NPCs and ambient creatures.
Random teleporting looks unnatural. Small angle changes each frame create smooth, organic movement.
When NPCs hit the edge, turn them around! Otherwise they'll wander off forever.
This "smooth random walk" algorithm is inspired by how real animals explore! It's called a "correlated random walk" in biology.
The real magic: combining behaviors! NPCs switch between states based on conditions.
Each state has its own behavior. PATROL walks waypoints, CHASE follows the player, IDLE stands still.
Conditions trigger state changes. "If player is close, switch to CHASE. If player escapes, go back to PATROL."
This is exactly what GameBuilder's Follow behavior does! Set the Follow Range to control when enemies start chasing.
GameBuilder's Follow behavior uses hysteresis too! Enemies start chasing at Follow Range, but only stop when you escape to 1.5x that distance. This prevents jittery switching!
Choose different behaviors and watch how they combine. This is how real game AI works!
Pace = Patrol, Follow = Chase, Jump = Bouncy enemy, Stationary = Hazard
Speed controls how fast. Pace Distance sets patrol range. Follow Range sets detection distance.
NPCs can Wander randomly! Set wander radius and speed for villagers and shopkeepers.
In GameBuilder, click Objects → Enemies → Enemy Templates to create enemies with these exact behaviors. Try making a guard that patrols, then chases when you get close!
You've learned the core movement patterns that bring game characters to life. These same techniques power everything from simple mobile games to AAA titles!
In GameBuilder, set behavior to "Pace" and adjust "Pace Distance" to control how far enemies walk.
Set behavior to "Follow" and adjust "Follow Range" - enemies ignore you until you get close!
The "Jump" behavior makes enemies hop around - set "Jump Power" for height.
In top-down mode, NPCs can "Wander" randomly around their starting spot.
GameBuilder has these exact patterns built in: