Hello,
I have some gameobjects in my Scene, I want all of them on a certain height.
The lowest bottom must have this height. For a 3D visual in a 2D Scene, so no rigidbody…
I try to calculate the bottom:
public static float CalculateBottom(GameObject go, float y)
{
Bounds _bounds = new Bounds(Vector3.zero, Vector3.zero);
foreach (MeshFilter filter in go.GetComponentsInChildren<MeshFilter>())
{
if (filter.mesh != null)
_bounds.Encapsulate(filter.mesh.bounds);
}
foreach (Renderer renderer in go.GetComponentsInChildren<Renderer>())
{
_bounds.Encapsulate(renderer.bounds);
}
Debug.Log("The local bounds of this model is " + _bounds);
Vector3 extents = _bounds.extents;
Vector3 center = _bounds.center;
//Vector3 top = new Vector3(center.x, center.y + extents.y, center.z);
//Vector3 left = new Vector3(center.x - extents.x, center.y, center.z);
//Vector3 right = new Vector3(center.x + extents.x, center.y, center.z);
Vector3 bottom = new Vector3(center.x, center.y - extents.y, center.z);
//return bottom.y;
//return -Mathf.Max(y, extents.y*2);
// return -extents.y*2;
//return y-extents.y;
return y-(center.y - extents.y);
}
I try to calculate it and set the result to Y (in some Update Routine).
All meshes etc are in childs of the gameobject.
Update Routine sets the Position:
PlayerHeadY = Globals.CalculateBottom(PlayerHead, PlayerHead.transform.position.y);
TalkerHeadY = Globals.CalculateBottom(TalkerHead, TalkerHead.transform.position.y);
PlayerHead.transform.position = new Vector3(-0.3f, PlayerHeadY, 0.5f);
TalkerHead.transform.position = new Vector3(0.3f, TalkerHeadY, 0.5f);
I cannot get both have their bottom on the same height. What am I doing wrong?
Thanks a lot!