- Home /
How to get the center point of an object without any renderers?
For a GameObject
with a Renderer
component, calling renderer.bounds.center
will return the center point of that game object.
However, I have a root GameObject
that just serves as the parent Transform
of many other GameObject
s:
+ Parent without renderer
- Child model
- Child model
- Child model
How can I retrieve the center of this parent object GameObject
as a whole (including its children)?
Answer by Bonfi_96 · Mar 17, 2016 at 07:18 PM
You should try to average the children's center coordinates
P: ( (x1+x2+x3)/3; (y1+y2+y3)/3; (z1+z2+z3)/3)
Id be curious to know how the editor works out where to place the handle in scene view, of an object with children.
I've tried now, With empty gameobjects the handle stays in place, while if the children had meshes in them the handle is placed in the middle. For sure it's not averaging the center of the children (if you put many childrens on one side and one on the other the handle is not closer to the side with more objects), I believe it's putting together the various bounding boxes of the various children and using the center of that big box as the place of the handle, not sure though
Answer by peverbian · Mar 20, 2016 at 04:07 PM
You want to average just the max and min in each direction, So something like P: (Max(x1, x2, x3) + Min(x1, x2, x3))/2; (Max(y1, y2, y3) + Min(y1, y2, y3))/2; (Max(z1, z2, z3) + Min(z1, z2, z3))/2; ,