0
0
Unityframework~20 mins

Raycasting for detection in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Raycasting Master
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 raycast hit check?

Consider the following Unity C# code snippet that casts a ray forward from the origin. What will be printed to the console?

Unity
Ray ray = new Ray(Vector3.zero, Vector3.forward);
if (Physics.Raycast(ray, out RaycastHit hit, 10f))
{
    Debug.Log("Hit: " + hit.collider.name);
}
else
{
    Debug.Log("No hit");
}
ANo hit
BHit: Wall
CHit: Player
DNullReferenceException
Attempts:
2 left
💡 Hint

Think about what objects are in front of the origin at runtime.

🧠 Conceptual
intermediate
1:30remaining
Which statement about RaycastHit is true?

Choose the correct statement about the RaycastHit struct in Unity.

A<code>RaycastHit.distance</code> gives the distance from the ray origin to the hit point.
B<code>RaycastHit.collider</code> is null if the ray hits an object.
C<code>RaycastHit.normal</code> is the direction of the ray.
D<code>RaycastHit.point</code> is always the origin of the ray.
Attempts:
2 left
💡 Hint

Think about what information you want after a ray hits something.

🔧 Debug
advanced
2:30remaining
Why does this raycast always miss?

Look at this code snippet. The raycast never detects any objects even though there are colliders in front of the camera. What is the problem?

Unity
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, 5f))
{
    Debug.Log("Hit: " + hit.collider.name);
}
else
{
    Debug.Log("No hit");
}
AThe ray origin is inside a collider causing immediate miss.
BThe ray direction is backwards.
CThe ray length is too short to reach the objects.
DPhysics.Raycast requires a layer mask to detect objects.
Attempts:
2 left
💡 Hint

Check the distance parameter and the scene setup.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this raycast code

Which option contains the correct syntax to perform a raycast and store the hit info?

Unity
RaycastHit hit;
bool isHit = Physics.Raycast(transform.position, transform.forward, hit, 10f);
Abool isHit = Physics.Raycast(transform.position, transform.forward, hit);
Bbool isHit = Physics.Raycast(transform.position, transform.forward, hit, 10f);
Cbool isHit = Physics.Raycast(transform.position, transform.forward, ref hit, 10f);
Dbool isHit = Physics.Raycast(transform.position, transform.forward, out hit, 10f);
Attempts:
2 left
💡 Hint

Remember how to pass parameters by reference in C# for out variables.

🚀 Application
expert
3:00remaining
How many objects will be detected by this raycast?

Given this code that casts a ray and collects all hits, how many objects will be detected if there are 3 colliders aligned in the ray's path within 15 units?

Unity
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit[] hits = Physics.RaycastAll(ray, 15f);
int count = hits.Length;
Debug.Log("Objects hit: " + count);
A1
B3
C0
DDepends on the order of colliders
Attempts:
2 left
💡 Hint

Think about what Physics.RaycastAll returns.