0
0
Unityframework~10 mins

Raycasting for detection 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 cast a ray forward from the object.

Unity
Ray ray = new Ray(transform.position, transform.[1]);
Drag options to blanks, or click blank then click option'
Aback
Bup
Cforward
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform.up or transform.right instead of forward.
Forgetting to use transform before the direction.
2fill in blank
medium

Complete the code to check if the ray hits any object within 10 units.

Unity
if (Physics.Raycast(ray, out RaycastHit hit, [1])) {
    Debug.Log("Hit detected");
}
Drag options to blanks, or click blank then click option'
A5
B10
C15
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using a distance too small or too large for the intended detection.
Omitting the distance parameter.
3fill in blank
hard

Fix the error in the raycast code to correctly detect hits.

Unity
if (Physics.Raycast([1], out RaycastHit hit)) {
    Debug.Log("Object hit: " + hit.collider.name);
}
Drag options to blanks, or click blank then click option'
Aray
Bray.origin
Ctransform.position
Dray.direction
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only the origin or direction instead of the full ray.
Passing the transform position instead of a ray.
4fill in blank
hard

Complete the code to create a dictionary of objects hit by rays longer than 5 units.

Unity
var hits = new Dictionary<string, float>();
    hits[hit.collider.name] = hit.distance;;
if (hit.distance [1] 5) {
    Debug.Log("Far hit detected");
}
Drag options to blanks, or click blank then click option'
A;
B,
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma instead of a semicolon to end the dictionary entry.
Using less than operator instead of greater than.
5fill in blank
hard

Fill all three blanks to store hit objects with their names in uppercase and distances greater than 3.

Unity
var detected = new Dictionary<string, float> {
    {hit.collider.[1](), hit.[2]
};
if (hit.distance [3] 3) {
    Debug.Log("Detected " + hit.collider.name);
}
Drag options to blanks, or click blank then click option'
Aname.ToUpper
Bdistance
C>
Dtag
Attempts:
3 left
💡 Hint
Common Mistakes
Using tag instead of name.ToUpper().
Using less than operator instead of greater than.
Forgetting parentheses on ToUpper().