Bird
Raised Fist0
Unityframework~20 mins

Enemy patrol and chase patterns in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Enemy Patrol and Chase Patterns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this patrol position update?
Consider this Unity C# code snippet for enemy patrol between two points. What will be the enemy's position after one update if it starts at (0,0,0) and moves towards (5,0,0) with speed 2 and deltaTime 1?
Unity
Vector3 start = new Vector3(0,0,0);
Vector3 end = new Vector3(5,0,0);
Vector3 position = start;
float speed = 2f;
float deltaTime = 1f;
Vector3 direction = (end - position).normalized;
position += direction * speed * deltaTime;
Debug.Log(position);
A(2.0, 0.0, 0.0)
B(5.0, 0.0, 0.0)
C(1.0, 0.0, 0.0)
D(0.0, 0.0, 0.0)
Attempts:
2 left
💡 Hint
Think about how far the enemy moves in one second at speed 2 towards the target.
🧠 Conceptual
intermediate
1:30remaining
Which condition triggers enemy chase in this pattern?
In a typical enemy AI, which condition below best triggers the enemy to stop patrolling and start chasing the player?
APlayer is within a certain detection radius of the enemy.
BEnemy health is below 50%.
CEnemy has completed a full patrol cycle.
DPlayer is outside the game map boundaries.
Attempts:
2 left
💡 Hint
Think about what makes the enemy notice the player.
❓ Predict Output
advanced
2:00remaining
What is the enemy's final position after this chase update?
Given this code snippet where enemy chases player at speed 3 for 2 seconds, starting at (1,0,1) and player at (7,0,1), what is the enemy's position after update?
Unity
Vector3 enemyPos = new Vector3(1,0,1);
Vector3 playerPos = new Vector3(7,0,1);
float speed = 3f;
float deltaTime = 2f;
Vector3 direction = (playerPos - enemyPos).normalized;
enemyPos += direction * speed * deltaTime;
Debug.Log(enemyPos);
A(1.0, 0.0, 1.0)
B(7.0, 0.0, 7.0)
C(7.0, 0.0, 1.0)
D(7.0, 0.0, 4.0)
Attempts:
2 left
💡 Hint
Calculate the distance moved and check if enemy overshoots the player.
🔧 Debug
advanced
2:30remaining
What error does this patrol code cause?
This Unity C# code snippet tries to move enemy between two points but causes an error. What error is it?
Unity
Vector3[] points = {new Vector3(0,0,0), new Vector3(5,0,0)};
int index = 1;
Vector3 position = points[index];
float speed = 2f;
float deltaTime = 1f;
Vector3 direction = (points[index + 1] - position).normalized;
position += direction * speed * deltaTime;
index++;
if(index >= points.Length) index = 0;
ADivideByZeroException
BIndexOutOfRangeException
CNo error, runs fine
DNullReferenceException
Attempts:
2 left
💡 Hint
Check what happens when index reaches the last element.
🚀 Application
expert
3:00remaining
How many patrol points are visited in this loop?
An enemy patrols through points: (0,0,0), (3,0,0), (3,0,3), (0,0,3) in order, moving 1 unit per second. After 10 seconds starting at (0,0,0), how many unique patrol points has the enemy reached or passed?
Unity
Vector3[] patrolPoints = {
  new Vector3(0,0,0),
  new Vector3(3,0,0),
  new Vector3(3,0,3),
  new Vector3(0,0,3)
};
float speed = 1f;
float totalTime = 10f;
int currentIndex = 0;
Vector3 position = patrolPoints[0];
float distanceToNext;
float timeLeft = totalTime;
while(timeLeft > 0) {
  Vector3 nextPoint = patrolPoints[(currentIndex + 1) % patrolPoints.Length];
  distanceToNext = Vector3.Distance(position, nextPoint);
  if(distanceToNext <= speed * timeLeft) {
    timeLeft -= distanceToNext / speed;
    position = nextPoint;
    currentIndex = (currentIndex + 1) % patrolPoints.Length;
  } else {
    position += (nextPoint - position).normalized * speed * timeLeft;
    timeLeft = 0;
  }
}
Debug.Log(currentIndex);
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Calculate distances between points and how many can be reached in 10 seconds.

Practice

(1/5)
1. What is the main purpose of an enemy patrol pattern in Unity game development?
easy
A. To make the enemy jump randomly
B. To move the enemy between set points repeatedly
C. To make the enemy disappear and reappear
D. To make the enemy shoot projectiles continuously

Solution

  1. Step 1: Understand enemy patrol behavior

    Enemy patrol means moving the enemy between fixed points in a loop to simulate guarding or searching.
  2. Step 2: Identify correct behavior from options

    Only moving between set points repeatedly matches patrol behavior.
  3. Final Answer:

    To move the enemy between set points repeatedly -> Option B
  4. Quick Check:

    Enemy patrol = moving between points [OK]
Hint: Patrol means looping between points, not random actions [OK]
Common Mistakes:
  • Confusing patrol with attack or random movement
  • Thinking patrol involves shooting or disappearing
2. Which of the following is the correct way to check the distance between the enemy and player in Unity C#?
easy
A. float dist = enemy.position.Distance(player.position);
B. float dist = enemy.position - player.position;
C. float dist = Vector3.Distance(enemy, player);
D. float dist = Vector3.Distance(enemy.position, player.position);

Solution

  1. Step 1: Recall Vector3.Distance syntax

    The correct method to get distance between two Vector3 points is Vector3.Distance(point1, point2).
  2. Step 2: Check each option

    float dist = Vector3.Distance(enemy.position, player.position); uses Vector3.Distance with positions, which is correct. Others have syntax errors or wrong usage.
  3. Final Answer:

    float dist = Vector3.Distance(enemy.position, player.position); -> Option D
  4. Quick Check:

    Use Vector3.Distance(point1, point2) for distance [OK]
Hint: Use Vector3.Distance with positions, not subtract vectors directly [OK]
Common Mistakes:
  • Subtracting vectors without calling Distance method
  • Calling Distance on a Vector3 instance incorrectly
  • Passing transforms instead of positions
3. Given this code snippet for enemy chase, what will be the output if the player is 3 units away and chaseDistance is 5?
if (Vector3.Distance(transform.position, player.position) < chaseDistance) {
    Debug.Log("Chasing player");
} else {
    Debug.Log("Patrolling");
}
medium
A. No output
B. Patrolling
C. Chasing player
D. Error at runtime

Solution

  1. Step 1: Calculate distance and compare

    Distance is 3, chaseDistance is 5, so 3 < 5 is true.
  2. Step 2: Determine which branch runs

    Since condition is true, "Chasing player" is printed.
  3. Final Answer:

    Chasing player -> Option C
  4. Quick Check:

    Distance 3 < 5 triggers chase [OK]
Hint: Compare distance to chaseDistance to decide behavior [OK]
Common Mistakes:
  • Confusing less than with greater than
  • Assuming no output if condition true
  • Thinking code causes error
4. Identify the error in this patrol code snippet:
void Patrol() {
    int currentPoint = 0;
    transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].position, speed * Time.deltaTime);
    if (transform.position == patrolPoints[currentPoint].position) {
        currentPoint++;
        if (currentPoint >= patrolPoints.Length) {
            currentPoint = 0;
        }
    }
}
medium
A. currentPoint should be declared outside the method to keep its value
B. Vector3.MoveTowards is used incorrectly
C. Time.deltaTime should not be multiplied by speed
D. The if condition should use < instead of >

Solution

  1. Step 1: Check variable scope for currentPoint

    currentPoint is reset to 0 every time Patrol() runs, so it never increments properly.
  2. Step 2: Understand how to fix the scope issue

    Declaring currentPoint outside the method (as a class variable) preserves its value between calls.
  3. Final Answer:

    currentPoint should be declared outside the method to keep its value -> Option A
  4. Quick Check:

    Keep patrol index outside method to track progress [OK]
Hint: Keep patrol index as a class variable, not local [OK]
Common Mistakes:
  • Thinking MoveTowards usage is wrong
  • Confusing comparison operators in if
  • Misunderstanding Time.deltaTime usage
5. How can you combine patrol and chase behaviors so the enemy patrols until the player is within 4 units, then chases the player, and returns to patrol if the player moves away?
hard
A. Use a state variable to switch between patrol and chase based on distance checks
B. Call Patrol() and Chase() methods simultaneously every frame
C. Only use Chase() method and ignore patrol points
D. Use a timer to switch behaviors every 5 seconds regardless of player position

Solution

  1. Step 1: Understand behavior switching logic

    The enemy needs to switch between patrol and chase based on player distance dynamically.
  2. Step 2: Use a state variable to track current behavior

    A state variable (like enum or bool) can control which method runs each frame depending on distance.
  3. Step 3: Implement distance check to update state

    If player is closer than 4 units, set state to chase; else set to patrol.
  4. Final Answer:

    Use a state variable to switch between patrol and chase based on distance checks -> Option A
  5. Quick Check:

    State variable + distance check = behavior switch [OK]
Hint: Switch states based on distance, don't run both behaviors together [OK]
Common Mistakes:
  • Running patrol and chase at the same time causing conflicts
  • Ignoring player distance for switching
  • Using timers instead of distance checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard