Physics.OverlapSphere vs Raycast?

I was wondering on your guys opinions of Physics.OverlapSphere vs using raycasts in general, or when to use one or the other.

So far I know:
Physics.OverlapSphere works off layers, while raycasts can be used with tags.
Both can be used to find gameobjects within a radius.

Both take the same filtering parameters, and both have versions which can return what has been hit:


Are you talking splash damages occlusion, or detection in general?

Neither raycasts nor overlaps can be used with tags, but both can be used with layers.

OverlapSphere finds objects in a radius from a single point in space, while something like SphereCast finds objects in a direction by casting a ray with the size/radius of a sphere (“width” may be a more accurate word than “radius” though).

Here’s a visual example:

7758933--977091--upload_2021-12-23_18-22-49.png

4 Likes

Just detection in general

As their name suggest, they do not operate in the same way. OverlapSphere act as a dicreet, localized operation which return everything that match your filter in its radius, even objects that are behind each other. Whereas a Raycast will usually (it depend on the engine/module/parameters used) report only the collision on the nearest object and for a given direction, it belong to the bullet type collision detection category (or continuous, althought continuous collisions can be performed as a succession of discreet operations). Whether you use one or the other, or any other method provided by the Physic module, depend entirely on the context in which you make these detection or collision requests. Until you give us those contexts, we wont be able to answer you in a meaningfull manner. Are you trying to find a ledge you can climb on, are you trying to shoot a projectile at a target ? It all depend…