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.

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.
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
| Value | Start with | What it changes |
|---|---|---|
| patrolSpeed | 2 units/s | How fast the enemy walks its route and walks home. Slow enough to read, and clearly slower than a chase so the change is visible. |
| chaseSpeed | 4.5 units/s | How 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. |
| patrolRadius | 5 units | How 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. |
| sightRange | 7 units | How 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. |
| loseRange | 11 units | How 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. |
| senseRange | 2 units | How close the player must be to be noticed even from behind. It stops the player from standing right behind a guard without being found. |
| noticeTime | 0.5 s | The 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. |
| searchTime | 3 s | How 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. |
| leashRange | 20 units | How 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
- An enemy with a route of 5 on each side of home. Leave it alone for 20 seconds: It walks all the way to both ends and never past them.
- An enemy that has just reached the right end. Let it keep walking: It looks left and walks back.
- An enemy facing right, with the player 6 units ahead. Wait: It stands still for half a second and then starts chasing.
- An enemy facing right. The player stands 6 units behind it, then 2.5, then 1.5: The first two are not noticed, the last one is.
- An enemy with a wall between it and the player. The player stands 3 units away: The enemy keeps patrolling.
- An enemy chasing the player. The player is 9 units away, then 12: At 9 the chase goes on, at 12 it turns into a search.
- An enemy that has lost the player at 8 units. Wait without the player coming back: It goes to the place, waits, gives up after 3 seconds and walks home.
- An enemy searching. The player steps into sight: The chase starts again without a new pause.
- An enemy chasing a player who runs far away. Run for 30 seconds: The enemy never leaves the area around its home and gives up the chase.
- An enemy chasing a player who stands still 3 units away. A long step goes by: The enemy stops exactly on the player and stays there.
Works well with
- Knockback: Otherwise the enemy walks against its own push and the hit loses its weight. Run the push first. While it lasts, let the enemy neither walk nor change mode.
- Stagger and Poise: The enemy keeps its mode through the stagger, so a staggered chaser is still a chaser. Let a stagger stop the walk and the chase while it lasts, and pick up again after it.
- Health and Damage: Otherwise a player can shoot a guard from outside its sight and nothing happens. Let a hit on the enemy count as seeing the player, and send the enemy to noticing.
- Difficulty Settings: Sight and reaction time change how it feels to sneak. Speeds change whether running away works. Turn the ranges and the pause into difficulty settings, not the speeds.
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.
What was checked
- 17 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
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