- Home /
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:
It seems hacky - I feel like the
Physics.SphereCastAll
is not behaving as expectedIf 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.
Answer by rutter · Jul 05, 2014 at 09:37 PM
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.
Thank you! This looks like exactly what I need, and it is most likely more efficient as well. Thanks for the heads up on the caveats as well, although for my needs I don't think they will be a problem.