rbjacob
rbjacob

Reputation: 333

Unity overlap sphere detecting no collidiers

I have a problem concerning the overlap between two cubes. I have a level that is populated with many 1x1x1 cubes in 3D, the positions of which are changing. I want to test if, for a given position, two of the cubes are overlapping. To do this, I called Phsyics.OverlapSphereNonAlloc with a radius of .25 at the position (which is at the center of the overlapping cubes) to return a list of colliders. If more than one collider is returned, then two of the colliders are overlapping. That was the idea. The call instead returned no colliders. Here is a 2D cross-sectional illustration of what I am doing:

I believe that no colliders are found because the entire overlap sphere lies inside of the cube collider. But the unity docs say that Phsyics.OverlapSphere...

Returns an array with all colliders touching or inside the sphere.

I would've thought that, although Physics.OverlapSphere lies inside the cube collider, the cube collider is still technically inside the overlap sphere. Can anyone explain this to me or offer a workaround method to find an array of all the colliders at a given point?

Upvotes: 1

Views: 3097

Answers (1)

Hexagon789
Hexagon789

Reputation: 315

The issue stems from the way Unity has worded the docs. In your case the sphere is inside the collider, where overlapsphere returns colliders inside the sphere.

I am not sure what kind of scale we are operating on here, but one approach would be to use Vector3.Distance to compare all the blocks to your position. If you have thousands of blocks you could consider merging the two methods, where you have a large overlapsphere that then takes the result and narrows it down using the distance formula.

Hopefully this helps! :)

Upvotes: 1

Related Questions