- Home /
Find center of multiple meshes
Hi. I'm trying to find center of the selected gameobject (at runtime) that contains multiple childs (meshes). The functionality is similar to one that you can see in Unity Editor with transform's position gizmo.
The problem is that all object are displaced from null point (red ball in pictures). Tried using mesh.bounds, with max and min values, but still no luck.
Basically, I want to get red ball at position where the gizmo is.
Comment
Ok. Looks like I have solved this by myself. This code have no error checks but should be O$$anonymous$$ to get the idea :)
Vector3 getCenter(params $$anonymous$$eshFilter[] meshes)
{
List<CombineInstance> combine = new List<CombineInstance>();
foreach ($$anonymous$$eshFilter filter in meshes)
{
CombineInstance temp = new CombineInstance();
temp.mesh = filter.shared$$anonymous$$esh;
temp.transform = filter.transform.localToWorld$$anonymous$$atrix;
combine.Add(temp);
}
$$anonymous$$esh combined$$anonymous$$esh = new $$anonymous$$esh();
combined$$anonymous$$esh.Combine$$anonymous$$eshes(combine.ToArray());
combined$$anonymous$$esh.RecalculateBounds();
return combined$$anonymous$$esh.bounds.center;
}