1
\$\begingroup\$

I needed a way to check for all gameobjects within a set sphere of my player object.

I found Physics.SphereCastAll which will return a RaycastHit[], exactly what I wanted. But, I am having trouble understanding the method signature/docs.

Basically, it's the two things in the title I am confused on right now:

From Unity docs:

  • radius - The radius of the sphere.
  • maxDistance - The max length of the sweep.

Is 'max length of the sweep' a measurement of time (or degrees?)? I would have thought the length would be the same as the radius (or at least you could calculate it from the radius) but it obviously isn't what I think length means.

\$\endgroup\$
6
\$\begingroup\$

SphereCast methods take a sphere and slide it along a line to see where it hits objects along its travel.

The radius parameter is the size of the sphere.

The maxDistance parameter is how far the sphere should travel in the given direction.

To simply check for objects near your player, you want CheckSphere or OverlapSphere / OverlapSphereNonAlloc instead. These methods check for colliders intersecting a given sphere, and give you either a bool result of whether a collision was detected, or an array of the colliders overlapping the radius. They don't try to slide that sphere to find contact positions.

This is not equivalent to a SphereCast with a maxDistance of 0. Like a raycast, this will only detect the points where it begins to intersect with an object - not situations where it was already overlapping in its starting position.

\$\endgroup\$
3
  • \$\begingroup\$ is there much of a system impact difference between having the 0f maxDistance on SphereCast and the OverlapSphere. It seems id have to rewrite some code when I already have it working? thanks for your time as always \$\endgroup\$ – Big T Larrity May 9 '18 at 13:25
  • \$\begingroup\$ See the edit above. A SphereCast with maxDistance = 0 does not perform the job of checking for overlaps inside a radius. It's not a matter of performance - this method just doesn't return the kinds of results you're looking for. \$\endgroup\$ – DMGregory May 9 '18 at 13:31
  • \$\begingroup\$ ah, i understand now thanks. So if i used my existing method, and it the object was already inside my sphere at the start then it would never get detected. I will adjust the code to use Collider instead with the OverlapSphere. thanks muchly \$\endgroup\$ – Big T Larrity May 9 '18 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.