Tracking object volume with bounds

Hi,

I’m really new to Unity and this is more of a best practices question, to make sure I am going about development correctly.

Currently I am playing with dynamically generating content, in random locations and rotations. To prevent objects from colliding with one another since they are randomly placed, I add a box collier around the instantiated objects. I then retrieve the boxcollider bounds and make sure it does not intersect with any other bounds (I keep a List iterating over the list everytime I create another object), removing any objects that would otherwise overlap eachother.

All the above logic works fine. The question I have, is if this is really the best way to go about what I am attempting to accomplish? Keep in mind every instantiated object could potentially have to the n’th degree children, each with various colliders, hence why I opted for a box collider around the enter prefab. Also this could lead to somewhat sizable boxcolliders to wrap around the instantiated object. Is there a better method of doing this or am I on the right track?

Any input is welcome, I am really trying to do things correctly and not just make them work.

Thanks

If you just need the bounds of the object, you can use the Renderer’s bounds to get a box roughly equal to what’s displayed. But if you need to explicitly set the size of the bounds or your prefab lacks a visual component, a box collider seems like a great solution.

If you’re really concerned - you could store the value of the bounds after the object is placed / rotated / scaled and then destroy the box collider component.

1 Like

Iterating against every collider in the scene can get expensive as the number of colliders goes up. An alternative would be to use something like an overlap sphere to get the colliders in the local vicinity, then iterate through those. This lets you take advantage of the physics engines more optimum collider storage and bounds checking.

If might not matter, if in doubt profile.

1 Like