Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I get the bounds of a rigidbody's compound collider?

Discussion in 'Editor & General Support' started by JonathanBolten, Jan 17, 2013.

  1. JonathanBolten

    JonathanBolten

    Joined:
    Dec 15, 2012
    Posts:
    50
    How do I get the bounds of a rigidbody's compound collider?

    I've looked through the Unity documentation and googled looking for the answer and couldn't find anything about this. If I need something like that, would most people just grab the bounds from "renderer.bounds"?

    Thanks!

    edit: I just realized this post may be in the wrong catagory, not sure though. Should I have asked this in Unity Answers? Or the scripting forum? Whoops, sorry!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Loop through the children and get their bounds, and add them all together.

    --Eric
     
  3. JonathanBolten

    JonathanBolten

    Joined:
    Dec 15, 2012
    Posts:
    50
    Thanks! 'Adding' might not have been the most accurate word. I first tried adding all the sizes together and realized that was inflating too much (interior colliders extending length). Looking at the documentation, 'Encapsulate' is what I needed to do. For anyone needing to do this in the future, the code is something like:
    Code (csharp):
    1.  
    2.         Collider[] myColliders = GetComponentsInChildren<Collider> ();
    3.         Bounds myBounds = new Bounds (transform.position , Vector3.zero);
    4.         foreach (Collider nextCollider in myColliders)
    5.         {
    6.             myBounds.Encapsulate (nextCollider.bounds);
    7.         }
    8.         Debug.Log (myBounds);
    9.  
    The myBounds variable is printing out nicely to the console, and changing the rotation of the parent is increasing/decreasing the size of the AABB! Woot! :)
     
    sunrisebacon and gauravkumar37 like this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Indeed, Bounds.Encapsulate is what I was referring to; sorry for not being more specific. Glad you found it anyway. :)

    --Eric
     
  5. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    Since this answer is still valid 7 years later, I wanted to correct something:

    If your colliders do not encapsulate the transform.position of your rigidbody, the solution above will not give the correct results. Here's a utility method that works even in that case:

    Code (CSharp):
    1.       public static Bounds GetCombinedBoundingBoxOfChildren(Transform root)
    2.         {
    3.             if (root == null)
    4.             {
    5.                 throw new ArgumentException("The supplied transform was null");
    6.             }
    7.  
    8.             var colliders = root.GetComponentsInChildren<Collider>();
    9.             if (colliders.Length == 0)
    10.             {
    11.                 throw new ArgumentException("The supplied transform " + root?.name + " does not have any children with colliders");
    12.             }
    13.  
    14.             Bounds totalBBox = colliders[0].bounds;
    15.             foreach (var collider in colliders)
    16.             {
    17.                 totalBBox.Encapsulate(collider.bounds);
    18.             }
    19.             return totalBBox;
    20.         }
     
    MaxEden, Ninjars and Wappenull like this.