Rigidbody: Raycasts - Gr8-Tools/game-developer-roadmap-doc GitHub Wiki
Unity Game Development Course: Rigidbody Casts and Overlaps
Total time: 9 hours
In this lesson, we will explore the Rigidbody casts and overlaps available in Unity's Physics system. We will discuss the different types of casts and overlaps, explain their use cases, and provide examples of their implementation using Unity's tools.
1. Key Definitions and Formulas
1.1 Raycast
- A
Raycast
is a line segment that starts at a specific point and goes in a specific direction. - It is used to detect objects along its path, typically used for line-of-sight checks, shooting mechanics, etc.
- Formula:
Physics.Raycast(origin, direction, distance, layerMask)
1.2 Linecast
- A
Linecast
is similar to a Raycast, but it checks every point along the line segment. - It is used to detect objects in a straight line between two points, typically used for obstacle avoidance, pathfinding, etc.
- Formula:
Physics.Linecast(start, end, layerMask)
1.3 Spherecast
- A
Spherecast
is similar to a Raycast, but it uses a sphere instead of a line segment. - It is used to detect objects within a sphere-shaped volume, typically used for character controller collision detection, physics-based interactions, etc.
- Formula:
Physics.SphereCast(origin, radius, direction, out hitInfo, maxDistance, layerMask)
1.4 OverlapSphere
- An
OverlapSphere
detects all colliders within a sphere-shaped volume. - It is used to find nearby objects or to check if a specific point is overlapped by any colliders, typically used for area-of-effect abilities, trigger zone checks, etc.
- Formula:
Physics.OverlapSphere(center, radius, layerMask)
Examples:
- Raycast Example:
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
// Do something with the hit object
Debug.Log("Hit object: " + hit.collider.gameObject.name);
}
- Linecast Example:
RaycastHit hit;
if (Physics.Linecast(startPoint, endPoint, out hit))
{
// Do something with the hit object
Debug.Log("Hit object: " + hit.collider.gameObject.name);
}
2. Algorithms (if used)
This section is skipped as Unity already provides built-in implementations for the discussed casts and overlaps.
3. Internet Resources for Further Learning
Links:
- Unity Documentation: Physics (Methods) - Link
Videos:
- Unity Physics: (Raycasts) - Link
Time: 40 minutes
- Unity: Raycast/BoxCast/SphereCast - Link
Time: 20 minutes
4. Practical Skill Improvement Tasks
Cube-game
Hit targets
Let's change the essence of the game. Now our task will be to “hit with a shot” as many targets as possible within the allotted time.
- Add a “sight” in the middle of the camera (first person). To do this, we use some kind of texture, which we attach to the UI element in the middle of the camera Canvas
- At the moment when the sight passes the target, we must “highlight” the target (any convenient effect, for example, changing the color of the target material). The target is illuminated the entire time the scope passes the target.
- The moment you press the left mouse button, a shot occurs. The shot causes the target to instantly die (and therefore spawn a new one).
Time: 3 hours
Traps
Let's create new one type of objects: traps. Every new match started different count of traps (from 5 to 10) spawned on the field in random places (and random rotation). The task of trap - catch player and disable it's movement for a few seconds (i.e., 2 seconds). When trap is active it's looking forward SphereCasting each FixedUpdate cylce, trying to catch the Character. If SphereCast detects the player:
- It makes Character disable for moving ("freeze") for a few seconds
- Once the Character caught the trap locking it self to continue catching the Character till the Character leaves the zone of catching (in that way the Character will have an opportunity on "unfreezing" to leave the trap).
Time: 5 hours