0
0
Unityframework~10 mins

Obstacle avoidance 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 object move forward continuously.

Unity
void Update() {
    transform.Translate(Vector3.[1] * Time.deltaTime);
}
Drag options to blanks, or click blank then click option'
Aleft
Bforward
Cright
Dup
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vector3.up moves the object upwards instead of forward.
Using Vector3.right moves the object sideways.
2fill in blank
medium

Complete the code to detect obstacles using a raycast.

Unity
if (Physics.Raycast(transform.position, transform.forward, [1])) {
    Debug.Log("Obstacle detected!");
}
Drag options to blanks, or click blank then click option'
A5f
BVector3.forward
CTime.deltaTime
Dtransform.position
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vector3.forward as distance causes a type error.
Using Time.deltaTime is incorrect because it is a time value, not distance.
3fill in blank
hard

Fix the error in the code to rotate the object away from an obstacle.

Unity
if (Physics.Raycast(transform.position, transform.forward, 5f)) {
    transform.Rotate(0, [1], 0);
}
Drag options to blanks, or click blank then click option'
ATime.deltaTime
B"90"
CVector3.up
D90
Attempts:
3 left
💡 Hint
Common Mistakes
Using "90" (string) causes a compile error.
Using Vector3.up is incorrect because Rotate expects float angles.
4fill in blank
hard

Fill both blanks to make the object stop moving when an obstacle is detected.

Unity
void Update() {
    if (Physics.Raycast(transform.position, transform.forward, [1])) {
        speed = [2];
    } else {
        speed = 5f;
    }
    transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Drag options to blanks, or click blank then click option'
A5f
B0
C10f
D-5f
Attempts:
3 left
💡 Hint
Common Mistakes
Setting speed to a negative value causes backward movement.
Using a different raycast distance than the movement check causes inconsistency.
5fill in blank
hard

Fill all three blanks to make the object avoid obstacles by turning right and moving forward.

Unity
void Update() {
    if (Physics.Raycast(transform.position, transform.forward, [1])) {
        transform.Rotate(0, [2], 0);
    }
    transform.Translate(Vector3.forward * [3] * Time.deltaTime);
}
Drag options to blanks, or click blank then click option'
A5f
B90
C3f
D10f
Attempts:
3 left
💡 Hint
Common Mistakes
Using a negative rotation angle turns left instead of right.
Using a speed of 0 stops the object from moving.