Axis aligned boundingbox around all children

A problem that keeps hauting me for years:

I have a GameObject “Master”. Master has multiple children. Now I want to create a bounding box around Master that encapsulates all children. This is easily done with this code…

var grpBounds = new Bounds() { center = master.transform.position };
 foreach (var meshrenderer in master.transform.GetComponentsInChildren<MeshRenderer>())
 {
     grpBounds.Encapsulate(meshrenderer.bounds);
 }
 boundingbox.localScale = grpBounds.size;
 boundingbox.position = grpBounds.center;

… The problem is that “Encapsultate()” draws bounds in global space. The previous code works fine, as long Master is aligned with global space. But if Master is rotated, the boudns will be still aligned with global space making the boundingbox way to big.

One solution for this is to align master globally, while bounds are calculated and then rotate everything back, with the bounds as child of master. This is a very awful solution though, and I’m sure there is a better one somewhere

This is how it looks like aligned (https://i.sstatic.net/EaRHYIZP.png)
This is how it looks like, when master is rotated (https://i.sstatic.net/51ggB8KH.png)
This is how I want it to look like, when master is rotated (https://i.sstatic.net/kW7Wiyb8.png)

If pic don’t work, here’s the original post on StackOverflow:

Yes, that’s how Axis Aligned bounding boxes work? It’s covered in the docs here: Unity - Scripting API: Bounds

… it does in global space, not in local space. Please look at the screenshots, linked at the end of the post, to understand my issue.

None of your links work.

Weird, they work for me. I edited the post and pasted the direkt link. Maybe copy paste will work.

Here’s the issue on Stackoverflow: unity game engine - Axis aligned bounds around all children - Stack Overflow

Pics should be visible here

Still doesn’t work:

Nonetheless, as per the docs, the bounds do not rotate with the object. They encapsulate the object on the global axis, expanding if the rotated object would be larger than were it not rotated.

Indeed! But I’m looking for a solution to this problem.

Well using Unity’s bounds won’t work here in that case. You would probably need to define your own bounds - or perhaps size - per-object, and accumulate those values to produce a box of the right size, and then rotate that based on the root object.

Unless these objects are not dynamically changing, in which, just define your own size that encompasses everything and use that.