Understanding Spherecasts in Unity
from:http://theantranch.com/blog/understanding-spherecasts-in-unity
Understanding Spherecasts in Unity
SphereCast and all related pages are confusing.
The easiest way for me to describe Spherecasts in the shortest terms possible is to post my bug report:
1) What happened
SphereCast and all related pages are confusing.
http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html
This is what’s in the documentation:
–
Description
Casts a sphere against all colliders in the scene and returns detailed information on what was hit.
This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast.
–
We had a long conversation [on IRC] about casting and the confusion in the documentation and there is one fundamental misunderstanding about how these volume casts work that is not addressed in the docs. This is closely related to what the user’s expectations of Spherecasts are.
Most users come across SphereCast and don’t understand it because they feel that SphereCasting should be “like a ray” but casting out in a sphere around the origin – Like OverlapSphere but instead of returning Collider[] it returns RaycastHit[].
This is not true, tho’ people try to use it like this.
SphereCast moves a Sphere volume down the direction and distance of a ray and detects what it bumps into.
The following description: “Casts a sphere against all colliders in the scene …” is under-descriptive and confusing.
This needs to be something along the lines of: “Casts a sphere (origin, radius) along a ray (origin, direction, distance) using layers (layerMask) to return an array (out hitInfo) containing detailed information on each collider which was hit by the sphere while traveling along the ray.”
If you google/search “SphereCast” on the forums and Unity Answers, you will see that many (most?) people don’t understand SphereCast and are using it improperly.
–
I think it’s important to note that many users want to check for hits within a sphere from an origin. OverlapSphere does do much of this, but I found as it returns Collider[], not RaycastHit[], one also needs additional information from methods such as Collider.ClosestPointOnBounds to find the something approximating a “hit location”. An even simpler way to check for colliders within a sphere is to useCheckSphere. This simply returns true if there are any colliders overlapping the sphere, so this does work, is simple, but doesn’t return any information about the collider or colliders within the sphere.