Understanding Spherecasts in Unity

SphereCast-Example

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 use CheckSphere. 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.

4 Comments

GaRzY

about 5 years ago Reply

Thanks for your explanation, I was confused but now I understand perfectly!! Great!!

Francesco

about 5 years ago Reply

Thank you!!! NOW it's really clear!!!!

Francesco

about 5 years ago Reply

If i use a Vector 3 for direction ed i write Vector3 direction=new Vector3(1,0,0), i am projecting a sphere along x direction, but i f i want a sphere in the middle of x-z plane what must i do? Thanks in advance

Adam

about 5 years ago Reply

My first question would be: "What are you trying to do as your final outcome?" - just to make sure that spherecast is the correct solution.

Spherecast is meant for checking the space between two points in the scene volume.

Spherecast will send a sphere, defined by radius, down a line, starting at origin and defined by direction, until the sphere reaches a point on that line defined by distance from origin. Along that line, the physics engine checks to see if the sphere collides with anything within that volume of the sphere.

Think of this as sliding a bead down a string and checking to see if it bumps into anything.

One of the most important characteristics about spherecast is that movement down a line...

A spherecast must move from one point to another.

If you simply want to check a volume around a point, you should look into overlapsphere or checksphere. These are both simple lookups in the docs.

Now, if you want to know how to START the checksphere at an ORIGIN that's in the middle of the x-z plane, you simply need to set your ORIGIN point to the middle of the x-z plane. Presuming by "middle of the x-z plane", you mean the origin point of the world, that would mean your ORIGIN point in your spherecast would be written like this:

SphereCast (new Vector3 (0, 0, 0), myRadius, myDirection, out hitInfo, myDistance, myLayers);

I suppose it would be possible to *fake* a checksphere with spherecast by giving an incredibly tiny distance (0.001) so it won't travel very far, but I'm unclear if this will cause any problems.

-

If I've failed to understand what you're asking, try me again!

Leave a Comment

Please be polite. We appreciate that. Your email address will not be published and the required fields are marked.