Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set the size of a GameObject's BoxCollider2D based on the bounds of child BoxCollider2Ds?

Discussion in 'Editor & General Support' started by Rob-Meade, Feb 28, 2019.

  1. Rob-Meade

    Rob-Meade

    Joined:
    Oct 27, 2016
    Posts:
    56
    Hi,

    I have a parent GameObject with a number of child GameObjects. Each child GameObject has a BoxCollider2D. I would like to add a BoxCollider2D to the parent GameObject and set its size/offset appropriately to effectively contain all of the child BoxCollider2Ds.

    I am currently taking the bounds of the child BoxCollider2Ds and using the Encapsulate method to expand the size of the parent BoxCollider2D, however, it isn't aligning correctly(perfectly). I think the reason for this is because some of the child BoxCollider2Ds are also using an offset, so I think the position isn't being taken into account and this is causing the very slight issue.

    The code is as follows;

    Code (CSharp):
    1. private void Start()
    2. {
    3.     _boxCollider2D.size = GetChildBoxCollider2DBounds().size;
    4. }

    Code (CSharp):
    1. private Bounds GetChildBoxCollider2DBounds()
    2. {
    3.     BoxCollider2D[] boxColliders = GetComponentsInChildren<BoxCollider2D>();
    4.  
    5.     if (boxColliders.Length > 0)
    6.     {
    7.         Bounds bounds = boxColliders[0].bounds;
    8.         for (int i = 1, ni = boxColliders.Length; i < ni; i++)
    9.         {
    10.             bounds.Encapsulate(boxColliders[i].bounds);
    11.         }
    12.         return bounds;
    13.     }
    14.     else
    15.     {
    16.         return new Bounds();
    17.     }
    18. }
    The outcome of this appears as follows;

    upload_2019-2-28_16-41-4.png

    Whilst only very slightly out, as you can see the parent BoxCollider2D is slightly lower than it should be.

    Is there anything anyone can think of that I need to consider/address in the above code which would correct this?

    Thanks in advance for your time/help :)

    ----------

    Regarding the offsets, the bottom two BoxCollider2Ds have a offset on Y of 0.03125, and the very top BoxCollider2D has a Y offset of 0.015625, the rest do not have an offset.

    Interestingly, if I set the parent BoxCollider2D's Y offset to 0.015625 manually, it aligns perfectly. I'm still trying to work the math out on why that is at this time. It is, of course, half of the value of the other offset value but I'm still struggling with this. I don't want to have to set this value manually, and instead have it calculated in the above code.

    ----------

    Updating the following to set the parent BoxCollider2D's offset to the Y value of the Center of the Bounds seems to resolve the issue;

    Code (CSharp):
    1.  
    2. private void Start()
    3. {
    4.     Bounds boxColliderBounds = GetBoxCollider2DBounds();
    5.  
    6.     Vector2 offset = new Vector2(0f, (boxColliderBounds.center.y - transform.position.y));
    7.  
    8.     _boxCollider2D.size = boxColliderBounds.size;
    9.     _boxCollider2D.offset = offset;
    10. }
    11.  
    I still don't really understand why I need to do this though, as I don't need to for the X offset for example?
     
    Last edited: Feb 28, 2019
  2. Rob-Meade

    Rob-Meade

    Joined:
    Oct 27, 2016
    Posts:
    56
    Whilst this seemed to work for growing the parent's BoxCollider2D component, it hasn't worked very well for shrinking it again. GetComponentsInChildren includes the component on the parent as well as the children, so I'm getting the full sized BoxCollider2D from the parent which then sets the size when it should be getting smaller.

    As a bit of a hack I've added a couple of lines to shrink the parent's BoxCollider2D down before running the above code again, this does work, but the parent's BoxCollider2D is now longer in the correct position, leaving the child BoxColliders on an end sticking out.

    There must be an easier way to achieve this, I just want one BoxCollider2D to surround, precisely, all child BoxCollider2Ds, and, should any of those be destroyed, resize (correctly). I feel like I am asking for the moon on a stick with this! lol