6
\$\begingroup\$

2D game with Unity. Have a parent game object, with several children objects scattered all over the scene.

I want to calculate the bounding rectangle of the parent game object.

Let the black dots be the children objects. The red rectangle is the bounding box. I am not really interested in the position - I only want the dimensions of such box.

Of course, the bounding box should also consider the bounding boxes of the children... and so on.

enter image description here


How can I achieve this? I heard that Renderer has a bounds property... but my parent object doesn't have a renderer. And when I put one, the value is 0 anyway.

\$\endgroup\$

4 Answers 4

9
\$\begingroup\$
Bounds GetMaxBounds(GameObject g) {
   var b = new Bounds(g.transform.position, Vector3.zero);
   foreach (Renderer r in g.GetComponentsInChildren<Renderer>()) {
     b.Encapsulate(r.bounds);
   }
   return b;
}
\$\endgroup\$
4
\$\begingroup\$

The answer provided by Alex Merlin gives a false result when the children's bounding box doesn't contain the pivot (transform.position) of the parent.

The following should always be correct

Bounds GetMaxBounds(GameObject g) {
   var renderers = g.GetComponentsInChildren<Renderer>();
   if (renderers.Length == 0) return new Bounds(g.transform.position, Vector3.zero);
   var b = renderers[0].bounds;
   foreach (Renderer r in renderers) {
     b.Encapsulate(r.bounds);
   }
   return b;
}
\$\endgroup\$
2
\$\begingroup\$

if your 2D game is laying on the XY plane for example, all you have to do is to determine 4 points in Global/WorldSpace. These points will be the 4 corners of the red rectangle and their coordinates will be formed with:

  • max X coordinate over all the childrens
  • max Y coordinate over all the childrens
  • min X coordinate over all the childrens
  • min Y coordinate over all the childrens

When calculating the local edges coordinates for your children you can use their bounding box or bounding circle (center position +/- radius). All you have to do is to maintain 4 variables in which you save max and min x/y coordinates you found parsing all your children objects

Pseudocode will be something like:

foreach( child in parent )
{
    if ( child.min_x < min_x ) min_x = child.min_x;
    if ( child.min_y < min_y ) min_y = child.min_y;
    if ( child.max_x > max_x ) max_x = child.max_x;
    if ( child.max_y > max_y ) max_y = child.max_y;
}

so now you'll have these 4 values let's form the 4 points (pseudo code):

top_left = Point2D(min_x, max_y);
top_right = Point2D(max_x, max_y);
bottom_right = Point2D(max_x, min_y);
bottom_left = Point2D(min_x, min_y);

So now that you have the corners of the rectangle, calculating the dimension is straight forward because you can consider 2D Points as 2D Vectors and calculate their distance to determine the edges of the red box. So you can simple calculate a difference between the points or use the Unity built-in function Vector2.Distance like so:

float BaseOfRedBox = Vector2.Distance(bottom_right, bottom_left); // the base
float HeightOfRedBox = Vector2.Distance(bottom_right, top_right); // the height
float AreaOfRedBox = BaseOfRedBox * HeightOfRedBox; // the Area

You can do more stuff considering the 2D Points as vectors and determine 2D Normals and so...

All the calculation is made in WorldSpace if you need to transform the points back to screen space just use the Unity function camera.WorldToScreenPoint(...) where camera is your main orthographic camera.

\$\endgroup\$
1
\$\begingroup\$

I dont use unity but I believe all you would need to do is find the x and y value of your parent object, then the x and y value based on the bounding box being the second screen, for example enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .