Intro
In Unity, a SphereCast is a physics operation that “sweeps” a sphere through space along a specific path to detect collisions. It is useful when a Raycast isn’t precise enough.
Implementation (C#)
RaycastHit hit;
float radius = 0.5f;
float maxDistance = 10f;
if (Physics.SphereCast(transform.position, radius, transform.forward, out hit, maxDistance))
{
// Access the hit object
GameObject hitObject = hit.collider.gameObject;
Debug.Log("Hit: " + hitObject.name + " at distance: " + hit.distance);
}
Key Parameters
• Origin: The center point where the sphere starts. • Radius: The thickness of the sphere being cast. • Direction: The vector along which the sphere travels. • hitInfo: An out parameter that stores RaycastHit data if something is hit. • Max Distance: How far the sphere should travel before stopping. • Layer Mask: Used to ignore or target specific Layers .
Common Use Cases
• Ground Detection: Checking if a character is standing on a floor, especially on uneven terrain where a single ray might miss. • Projectile Collision: Simulated bullets or projectiles with physical size. • AI Pathfinding: Ensuring there is enough clearance for a character model to pass through a narrow gap. [1, 2, 9, 10, 11]



