Find objects overlapping a collider

Hi,

I’m trying to find all the gameobjects that are within range of the current collider. I don’t want to use event-handling methods like OnTriggerXXX/OnCollisionXXX, and I don’t want any of the objects to have rigidbodies either. All I want is given a collider, find all the other colliders that overlap it.

The closest thing I know of is Physics.OverlapSphere, but it’s limited to a sphere shape. I’d like to do something very similar with a collider.

Any ideas?

Well you know the size of the object and it's position. You could check if two objects are within' eachothers size area and position. Though, may I ask, why don't you want to use rigidbodies or colliders or OnCollision/Trigger?

You could do the Physics.OverlapSphere() first, then do the Bounds.Intersect() for anything it finds. It is probable that under the hood, Unity is iterating through all the colliders with OverlapSphere(), so it might not buy you anything. That is, if you do a distance check in your loop before you do the Bounds.Intersect() you may have similar performance.

1 Answer

1

http://docs.unity3d.com/Documentation/ScriptReference/Bounds.Intersects.html

Something like this?

Although if you used OverlapSphere you could put in a layermask. This function actually checks against bounding volumes, not colliders.

I haven't really played with changing the size of a bounding volume. You could supposedly have an empty object and set its bounding volume to any size you choose, using intersect to find a crossing.

[Bounds uses an AABB][1] [1]: http://docs.unity3d.com/ScriptReference/Bounds.html This means it only works if you want to check in a box shape that is rotated to be aligned with the world axis, otherwise you'll get less-predictable behaviour.