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
intermediate2: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);
Attempts:
2 left
💡 Hint
Think about how far the enemy moves in one second at speed 2 towards the target.
✗ Incorrect
The enemy moves from (0,0,0) towards (5,0,0) at speed 2 for 1 second, so it moves 2 units along the x-axis, ending at (2,0,0).
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about what makes the enemy notice the player.
✗ Incorrect
Enemies usually start chasing when the player comes close enough, inside a detection radius.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Calculate the distance moved and check if enemy overshoots the player.
✗ Incorrect
Enemy moves 3*2=6 units towards player from (1,0,1). Distance to player is 6 units, so enemy reaches exactly (7,0,1).
🔧 Debug
advanced2: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;Attempts:
2 left
💡 Hint
Check what happens when index reaches the last element.
✗ Incorrect
When index is at last element, index + 1 accesses out of array bounds causing IndexOutOfRangeException.
🚀 Application
expert3: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);Attempts:
2 left
💡 Hint
Calculate distances between points and how many can be reached in 10 seconds.
✗ Incorrect
Enemy moves along square edges of length 3 units each. Total perimeter is 12 units. In 10 seconds at speed 1, enemy covers 10 units, passing 3 points (0->3->3->0).