0
0
Unityframework~10 mins

Enemy patrol and chase patterns in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enemy patrol and chase patterns
Start
Patrol between points
Detect player nearby?
NoContinue patrol
Yes
Chase player
Player lost?
NoContinue chase
Yes
Return to patrol
Back to Patrol between points
The enemy moves between patrol points until it detects the player nearby, then chases the player until lost, then returns to patrol.
Execution Sample
Unity
void Update() {
  Patrol();
  if (PlayerInRange()) {
    ChasePlayer();
  }
}
Enemy patrols points and switches to chasing player when detected.
Execution Table
StepEnemy PositionPlayer PositionPlayer Detected?ActionOutput
1(0,0)(5,5)NoPatrol to next point (1,0)Enemy moves to (1,0)
2(1,0)(4,4)NoPatrol to next point (2,0)Enemy moves to (2,0)
3(2,0)(3,3)YesStart chasing playerEnemy starts chase
4(2,0)(3,3)YesMove towards player (2.7,0.7)Enemy moves closer to player
5(2.7,0.7)(3,3)YesMove towards player (3.3,1.4)Enemy moves closer to player
6(3.3,1.4)(6,6)NoPlayer lost, return to patrolEnemy returns to patrol point (3,0)
7(3,0)(6,6)NoPatrol to next point (4,0)Enemy moves to (4,0)
Exit(4,0)(6,6)NoPatrol continuesEnemy continues patrol
💡 Player is out of detection range, enemy resumes patrol.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
Enemy Position(0,0)(1,0)(2,0)(2,0)(2.7,0.7)(3.3,1.4)(3,0)(4,0)(4,0)
Player Position(5,5)(4,4)(3,3)(3,3)(3,3)(6,6)(6,6)(6,6)(6,6)
Player DetectedNoNoYesYesYesNoNoNoNo
Current ActionPatrolPatrolChaseChaseChaseReturn to PatrolPatrolPatrolPatrol
Key Moments - 3 Insights
Why does the enemy switch from patrolling to chasing at step 3?
At step 3, the player position is close enough to the enemy, so 'Player Detected?' becomes Yes, triggering the chase action as shown in the execution_table.
Why does the enemy stop chasing and return to patrol at step 6?
At step 6, the player moves out of detection range, so 'Player Detected?' is No, causing the enemy to stop chasing and return to patrol, as seen in the execution_table.
Does the enemy immediately chase the player if detected during patrol?
Yes, as soon as 'Player Detected?' is Yes during patrol (step 3), the enemy switches to chase mode, shown by the change in 'Current Action' in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the enemy's position?
A(2,0)
B(3,3)
C(2.7,0.7)
D(1,0)
💡 Hint
Check the 'Enemy Position' column at step 4 in the execution_table.
At which step does the enemy lose sight of the player and return to patrol?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Look for when 'Player Detected?' changes to No and 'Current Action' changes to 'Return to Patrol' in the execution_table.
If the player stayed within detection range at step 6, what would happen next?
AEnemy stops moving
BEnemy continues chasing the player
CEnemy returns to patrol
DEnemy moves randomly
💡 Hint
Refer to the 'Current Action' and 'Player Detected?' columns in the execution_table for steps 4 and 5.
Concept Snapshot
Enemy patrols between points.
If player detected nearby, enemy chases player.
If player lost, enemy returns to patrol.
Use distance checks to switch states.
Update enemy position each frame accordingly.
Full Transcript
This visual trace shows how an enemy in a game patrols between points and switches to chasing the player when detected nearby. The enemy moves step-by-step between patrol points until the player comes close enough. At that moment, the enemy starts chasing the player by moving towards the player's position. If the player moves out of detection range, the enemy stops chasing and returns to its patrol route. Variables like enemy position, player position, detection status, and current action are tracked at each step to understand the flow. This helps beginners see how patrol and chase patterns work in game programming.