Challenge - 5 Problems
Obstacle Avoidance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this obstacle detection code?
Consider the following Unity C# script snippet that uses a Raycast to detect obstacles in front of a moving object. What will be printed to the console if there is an obstacle 3 units ahead?
Unity
void Update() {
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 5f)) {
Debug.Log($"Obstacle detected at distance: {hit.distance}");
} else {
Debug.Log("No obstacle detected");
}
}Attempts:
2 left
💡 Hint
The Raycast checks for obstacles up to 5 units ahead and returns the closest hit distance.
✗ Incorrect
The Raycast detects the obstacle at 3 units ahead, which is within the 5 unit range, so it prints the exact distance.
🧠 Conceptual
intermediate1:30remaining
Which method best describes obstacle avoidance behavior in Unity?
In Unity, which approach is commonly used to make a character avoid obstacles while moving forward?
Attempts:
2 left
💡 Hint
Think about how to detect obstacles before hitting them and change direction.
✗ Incorrect
Raycasts allow the character to sense obstacles ahead and adjust movement to avoid collisions.
🔧 Debug
advanced2:30remaining
Why does this obstacle avoidance script fail to detect obstacles?
This Unity script is intended to detect obstacles and stop the character, but it never detects anything. What is the cause?
Unity
void Update() {
if (Physics.Raycast(transform.position, Vector3.up, out RaycastHit hit, 5f)) {
Debug.Log("Obstacle detected");
}
}Attempts:
2 left
💡 Hint
Check the direction vector used in the Raycast.
✗ Incorrect
The Raycast is cast upwards (Vector3.up) instead of forward, so it does not detect obstacles in front.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this obstacle avoidance code snippet
Which option contains the correct syntax to perform a Raycast and check if it hits an obstacle within 10 units?
Unity
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 10f)) { Debug.Log("Hit detected"); }
Attempts:
2 left
💡 Hint
The 'out' keyword must be used with variable declaration inside the method call.
✗ Incorrect
Option B correctly declares 'out RaycastHit hit' inside the Raycast call. Option B misses max distance parameter, Option B misses 'out', Option B misses type declaration.
🚀 Application
expert3:00remaining
What is the final position of the character after this obstacle avoidance logic runs?
A character starts at position (0,0,0) facing forward (z+). It moves forward 5 units unless an obstacle is detected within 3 units. If an obstacle is detected, it turns right 90 degrees and moves forward 2 units. There is an obstacle at (0,0,2). What is the character's final position after running this logic once?
Attempts:
2 left
💡 Hint
The obstacle is closer than 3 units ahead, so the character turns right and moves 2 units on x axis.
✗ Incorrect
The character detects the obstacle at 2 units ahead, so it turns right (x+ direction) and moves 2 units, ending at (2,0,0).