0
0
Unityframework~10 mins

Enemy patrol and chase patterns in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the enemy move between two points.

Unity
Vector3 targetPosition = points[[1]].position;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
Drag options to blanks, or click blank then click option'
A2
B1
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using an index out of range like 2 or 3 causes errors.
Starting at index 1 skips the first point.
2fill in blank
medium

Complete the code to check if the enemy reached the patrol point.

Unity
if (Vector3.Distance(transform.position, points[currentPoint].position) [1] 0.1f) {
    currentPoint = (currentPoint + 1) % points.Length;
}
Drag options to blanks, or click blank then click option'
A<
B>
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes the condition to never be true.
Using >= or <= changes the logic incorrectly.
3fill in blank
hard

Fix the error in the chase code to make the enemy follow the player.

Unity
transform.position = Vector3.MoveTowards(transform.position, [1], speed * Time.deltaTime);
Drag options to blanks, or click blank then click option'
Aplayer
BplayerPosition
Cplayer.transform
Dplayer.transform.position
Attempts:
3 left
💡 Hint
Common Mistakes
Using player or player.transform directly causes type errors.
Using playerPosition if not defined causes errors.
4fill in blank
hard

Fill both blanks to make the enemy switch from patrol to chase when close to the player.

Unity
if (Vector3.Distance(transform.position, player.transform.position) [1] chaseRange) {
    isChasing = [2];
}
Drag options to blanks, or click blank then click option'
A<=
Btrue
C>
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= reverses the logic.
Setting isChasing to false stops chasing.
5fill in blank
hard

Fill all three blanks to make the enemy stop chasing and resume patrol when the player is far.

Unity
if (Vector3.Distance(transform.position, player.transform.position) [1] chaseRange) {
    isChasing = [2];
    currentPoint = [3];
}
Drag options to blanks, or click blank then click option'
A>
Bfalse
C0
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > reverses the logic.
Setting isChasing to true keeps chasing.
Not resetting currentPoint causes patrol to start at wrong point.