Hey, I have this scene:
With the transform handle set to ‘center’. ‘GameObject’ is just an empty gameobject. How can I get the position of that center tool handle?
Hey, I have this scene:
With the transform handle set to ‘center’. ‘GameObject’ is just an empty gameobject. How can I get the position of that center tool handle?
You can’t. At least Unity doesn’t calculate that for you. In the editor it’s a feature that is built-into the editor.
The best approach would be to iterate over all Renderer in all child objects, gather the worldspace bounds values and combine them with [Bounds.Encapsulate][1].
Vector3 GetCenter()
{
var rends = GetComponentsInChildren<Renderer>();
if (rends.Length == 0)
return transform.position;
var b = rends[0].bounds;
for(int i = 1; i < rends.Length; i++)
{
b = b.Encapsulate(rends*.bounds);*
}
return b.center;
}
[1]: Unity - Scripting API: Bounds.Encapsulate