I have a root prefab where I want to add box-collider in runtime based on bounds of child prefabs with letter meshes combined in a word. On the picture I changed size and center manually to match bounds of characters.
This is what I tried:
Bounds bounds = new Bounds(parentObject.transform.position, Vector3.zero);
foreach (MeshFilter meshFilter in parentObject.GetComponentsInChildren<MeshFilter>())
{
bounds.Encapsulate(meshFilter.mesh.bounds);
}
BoxCollider collider = parentObject.AddComponent<BoxCollider>();
collider.center = bounds.center;
collider.size = bounds.size;
I’m getting enormous collider in this case. And it looks like somewhere wrong coordinates conversion.
How can I get size of meshes to calculate correctly offset for the next letter to add (currently I’m using hardcoded constant) ?
How can I calculate center and bounds for box collider in correct coordinate space ?