Spielerando

Enemy Patrol and Chase

An enemy walks a route back and forth, notices the player, pauses, chases, searches the last place it saw the player, then gives up and walks home. Noticing takes a shorter range than keeping the player, sight needs a clear line, and a leash around home stops a chase from dragging the enemy across the level.

Also called: Enemy AI, Patrol route, Guard AI, Aggro and leash, Detection and pursuit.

A stocky lantern-carrying troll guard lunges after a small hooded fox thief running across a snowy stone bridge, with the lantern's pale cone of light reaching the fox.
Mossgate Rounds, an invented low-poly stealth game. Invented game, image generated with AI.

Paste this to your AI

Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.

Add "Enemy Patrol and Chase" to my game.

What it should do: A patrolling enemy gives the player something to read, sneak past, lure or fight. The pause before the chase, the search and the walk home are what make the enemy feel alive instead of like a trap that always knows where you are.

Build it exactly as this state machine. First check that it fits my game, then build it like this:

```mermaid
stateDiagram-v2
  [*] --> patrol
  patrol: Patrolling
  noticing: Noticing
  chasing: Chasing
  searching: Searching
  returning: Walking home
  patrol --> patrol: walks the route
  patrol --> noticing: sees the player
  noticing --> noticing: pauses
  noticing --> chasing: pause is over
  noticing --> patrol: player gone
  chasing --> chasing: follows
  chasing --> searching: loses sight
  searching --> searching: looks around
  searching --> chasing: sees the player
  searching --> returning: gives up
  returning --> returning: walks home
  returning --> noticing: sees the player
  returning --> patrol: back on the route
```

Use these starting values:
- patrolSpeed: 2 units/s
- chaseSpeed: 4.5 units/s
- patrolRadius: 5 units
- sightRange: 7 units
- loseRange: 11 units
- senseRange: 2 units
- noticeTime: 0.5 s
- searchTime: 3 s
- leashRange: 20 units

Watch out for these common mistakes:
- One distance for noticing and keeping: Tell your AI: use a shorter range to start noticing and a longer range to keep the player.
- Seeing through walls: Tell your AI: sight needs a clear line, checked every tick, also while chasing.
- A chase that never ends: Tell your AI: give every enemy a leash around home and let it give up when the player is outside it.
- Jitter on top of the player: Tell your AI: never step farther than the gap, so the enemy stops on the spot.
- Knowing where the player is: Tell your AI: save the place only in ticks in which the player was seen, and search there.
- Placed outside the route: Tell your AI: turn toward the end it is heading for and walk back into the route first.

Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP

What it does

A patrolling enemy gives the player something to read, sneak past, lure or fight. The pause before the chase, the search and the walk home are what make the enemy feel alive instead of like a trap that always knows where you are.

How it runs, as a diagram

Everything it can be in, and what moves it on. Your AI builds from exactly this.

walks the routesees the playerpausespause is overplayer gonefollowsloses sightlooks aroundsees the playergives upwalks homesees the playerback on the routePatrollingNoticingChasingSearchingWalking home
How it runs, as a diagram
  • Patrolling → Patrolling: walks the route
  • Patrolling → Noticing: sees the player
  • Noticing → Noticing: pauses
  • Noticing → Chasing: pause is over
  • Noticing → Patrolling: player gone
  • Chasing → Chasing: follows
  • Chasing → Searching: loses sight
  • Searching → Searching: looks around
  • Searching → Chasing: sees the player
  • Searching → Walking home: gives up
  • Walking home → Walking home: walks home
  • Walking home → Noticing: sees the player
  • Walking home → Patrolling: back on the route

Good starting values

ValueStart withWhat it changes
patrolSpeed2 units/sHow fast the enemy walks its route and walks home. Slow enough to read, and clearly slower than a chase so the change is visible.
chaseSpeed4.5 units/sHow fast the enemy runs in a chase and to the last place it saw the player. Keep it a little under the player's top speed, so running away works and standing still does not.
patrolRadius5 unitsHow far the route reaches either side of home. Five gives a walk of ten units, long enough to slip past and short enough to be seen as a guarded spot.
sightRange7 unitsHow far ahead the enemy notices the player. Seven is far enough that the player has seen the enemy first and has time to choose, and near enough that sneaking past it is a risk.
loseRange11 unitsHow far the enemy still sees a player it has already noticed. It must be larger than the notice range, or an enemy at the edge flips between chasing and not chasing every tick. A smaller value counts as the notice range.
senseRange2 unitsHow close the player must be to be noticed even from behind. It stops the player from standing right behind a guard without being found.
noticeTime0.5 sThe pause between seeing the player and starting the chase. Half a second gives the player a moment to step back, and shows the enemy has noticed.
searchTime3 sHow long the enemy searches after losing the player before it gives up. Three seconds is enough to go to the last place seen and look around, and not enough to feel like it never lets go.
leashRange20 unitsHow far from home the enemy goes, and how far from home the player still counts. Past it the enemy gives up, so a long chase cannot pull it out of its area. A smaller value than the route counts as the route.

What usually goes wrong

One distance for noticing and keeping

With a single sight range, a player standing right at the edge is seen, then lost, then seen again in the next tick, and the enemy twitches between chasing and patrolling.

Fix: Tell your AI: use a shorter range to start noticing and a longer range to keep the player.

Seeing through walls

An enemy that only measures distance chases the player through the floor or a wall and attacks something it cannot see.

Fix: Tell your AI: sight needs a clear line, checked every tick, also while chasing.

A chase that never ends

The player runs, the enemy runs, and a few minutes later the enemy is on the other side of the level, ahead of other enemies, away from what it guarded.

Fix: Tell your AI: give every enemy a leash around home and let it give up when the player is outside it.

Jitter on top of the player

An enemy that always steps its full speed toward the player jumps past it and back on every tick when it arrives.

Fix: Tell your AI: never step farther than the gap, so the enemy stops on the spot.

Knowing where the player is

If the search heads for the player's real position, the enemy finds a player it can no longer see, and hiding never works.

Fix: Tell your AI: save the place only in ticks in which the player was seen, and search there.

Placed outside the route

An enemy dropped past the end of its route turns around at the end it is already beyond, or walks away from it forever.

Fix: Tell your AI: turn toward the end it is heading for and walk back into the route first.

How to check it works

Works well with

Common questions

how do i make an enemy patrol and chase the player

Give it five modes: patrolling, noticing, chasing, searching and walking home. Move between them on what the enemy can see, and keep a pause between seeing and chasing so the player can react.

why does my enemy keep switching between chasing and patrolling

It uses one sight distance, and the player stands right at its edge. Use a shorter distance to notice the player and a longer one to keep it.

how far should an enemy chase the player

Start with a leash of about four times the route, here 20 units. Past it the enemy gives up and walks home, so a player can lead enemies away and still come back to a quiet room.

what do i tell my ai to add enemy patrol and chase

Paste this: Add an enemy that walks a route of 5 units each side of home, notices the player 7 units ahead or 2 units behind, pauses half a second, chases up to 11 units and inside a leash of 20, searches the last seen place for 3 seconds, then walks home. Walls block sight.

Where it comes from

We could not find a reliable origin for this one yet.

Where this mechanic comes from has not been researched yet.

Games are named only as references. Names and marks belong to their owners, and nothing here is endorsed by them.

What was checked

This is what separates the blueprint from a catalogue entry: the values on this page come from code that runs.

Your AI gets the code

Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it