Search Unity

Tracking object volume with bounds

Discussion in 'Scripting' started by FloridaDev, Jul 28, 2015.

  1. FloridaDev

    FloridaDev

    Joined:
    Jul 28, 2015
    Posts:
    1
    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 <bounds> 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
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
    FloridaDev likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    FloridaDev likes this.