NPC Movement Patterns

How do game characters know where to go? Discover the simple rules behind smart-looking AI.

1

Patrol Movement

The simplest NPC behavior: walk between a set of waypoints, over and over.

Guard NPC
Waypoints
// Move toward current waypoint
if (distance_to(waypoint) < 5) {
  current_waypoint = (current_waypoint + 1) % waypoints.length;
}
move_toward(waypoints[current_waypoint], speed);

Waypoints

A list of (x, y) positions the NPC visits in order. When it reaches the last one, it goes back to the first.

Arrival Check

Check if NPC is "close enough" to the waypoint (within a few pixels), then switch to the next one.

Fun Fact

Guards in stealth games like Metal Gear Solid use patrol patterns. Speedrunners memorize these exact paths to slip past undetected!

2

Chase Behavior

Move your mouse (or tap) to control the player. Watch the enemy chase you!

Player (You)
Chasing Enemy
Distance: 0 px
// Calculate direction to player
dx = player.x - enemy.x;
dy = player.y - enemy.y;
distance = sqrt(dx*dx + dy*dy);

// Normalize and move
enemy.x += (dx / distance) * speed;
enemy.y += (dy / distance) * speed;

Direction Vector

Subtract positions (target - self) to get a vector pointing toward the target.

Normalize

Divide by distance to get a unit vector (length 1), then multiply by speed for consistent movement.

Fun Fact

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.

3

Flee Behavior

The opposite of chase - the NPC runs away from you! Move toward it to scare it off.

Player (You)
Scared NPC
NPC State
Idle
// Flee is just chase with reversed direction!
dx = npc.x - player.x; // Swapped!
dy = npc.y - player.y;
distance = sqrt(dx*dx + dy*dy);

if (distance < fear_radius) {
  npc.x += (dx / distance) * speed;
  npc.y += (dy / distance) * speed;
}

Just Flip It!

Flee uses the same math as chase, but subtract player from NPC instead of NPC from player.

Fear Radius

NPCs only flee when the player is within a certain distance. Otherwise they stay calm.

Fun Fact

In Minecraft, passive animals flee when attacked. The flee direction is randomized slightly so they don't all run in a straight line!

4

Wander Behavior

Random movement that looks natural. Great for idle NPCs and ambient creatures.

Wandering NPCs
// Smooth wander: slightly adjust angle each frame
angle += (random() - 0.5) * turn_speed;

npc.x += cos(angle) * speed;
npc.y += sin(angle) * speed;

Smooth vs Jerky

Random teleporting looks unnatural. Small angle changes each frame create smooth, organic movement.

Boundaries

When NPCs hit the edge, turn them around! Otherwise they'll wander off forever.

Fun Fact

This "smooth random walk" algorithm is inspired by how real animals explore! It's called a "correlated random walk" in biology.

5

State Machines

The real magic: combining behaviors! NPCs switch between states based on conditions.

Player (You)
Patrolling
Chasing
Guard State
PATROL
Detection Range: 150px | Distance: 0px
switch(state) {
  case "PATROL":
    patrol_movement();
    if (distance_to_player < detect_range)
      state = "CHASE";
    break;
  case "CHASE":
    chase_player();
    if (distance_to_player > detect_range * 1.5)
      state = "PATROL";
    break;
}

States

Each state has its own behavior. PATROL walks waypoints, CHASE follows the player, IDLE stands still.

Transitions

Conditions trigger state changes. "If player is close, switch to CHASE. If player escapes, go back to PATROL."

GameBuilder's "Follow"

This is exactly what GameBuilder's Follow behavior does! Set the Follow Range to control when enemies start chasing.

Fun Fact

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!

6

Mix and Match

Choose different behaviors and watch how they combine. This is how real game AI works!

🚶
Patrol
🏃
Chase
😨
Flee
🌀
Wander
Current Behavior
PATROL

In GameBuilder

Pace = Patrol, Follow = Chase, Jump = Bouncy enemy, Stationary = Hazard

Key Settings

Speed controls how fast. Pace Distance sets patrol range. Follow Range sets detection distance.

Top-Down Mode

NPCs can Wander randomly! Set wander radius and speed for villagers and shopkeepers.

Ready to Build?

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!

NPC Movement Master!

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!

0
Patterns Explored
0
NPC Interactions
0
Time Exploring

Pace = Patrol

In GameBuilder, set behavior to "Pace" and adjust "Pace Distance" to control how far enemies walk.

Follow = Chase

Set behavior to "Follow" and adjust "Follow Range" - enemies ignore you until you get close!

Jump = Bouncy

The "Jump" behavior makes enemies hop around - set "Jump Power" for height.

Wander for NPCs

In top-down mode, NPCs can "Wander" randomly around their starting spot.

Try It in GameBuilder!

GameBuilder has these exact patterns built in:

Create Your First Enemy
1 Click Objects → Enemies in the toolbar
2 Click Enemy Templates to open the editor
3 Set Behavior to "Pace" (patrol) or "Follow" (chase)
4 Adjust Speed and Pace Distance or Follow Range
5 Click in your level to place the enemy!
6 Bonus: Check "Stompable" to let players defeat it by jumping on it
More Discoveries

Suggest a Correction