How do you perform a stationary SphereCastAll?

I am attempting to find all colliders within a sphere volume. To do this I am using Physics.SphereCastAll. However, I do not need to send the sphere along a path and I am running into a few problems because of this.

Problems:

  • When distance is set to 0, no colliders are found.
  • When distance is set to a small value (.1), RaycastHits are only found at the borders of the sphere. The interior of the sphere does not trigger hits at the starting position.

The hit testing works as expected if I move the sphere beyond its starting position. For example, if I move the sphere back by a diameter, and set distance = diameter, I get the results I expect.

However, this is not ideal for two reasons:

  1. It seems hacky - I feel like the Physics.SphereCastAll is not behaving as expected
  2. If I have objects at the position where I am now starting the SphereCast, some of them will trigger hits. This is not desired because they are outside the real volume I am testing.

How can I accomplish this stationary SphereCast without changing the position? Should I be using some other method?

I can share more code or pictures if needed. The objects I am detecting hits on is a 3d grid of 1x1x1 cubes.

You want Physics.OverlapSphere. It sounds like it does pretty much exactly what you’re looking for.

There are a few caveats:

  • Instead of getting collision information for each hit, you just get an array of nearby colliders.
  • The sphere checks against the outer AABB of each collider, not necessarily the collider itself.

In most cases, those aren’t important concerns, but they’re good to know about.