Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created June 25, 2013 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimfleming/5855904 to your computer and use it in GitHub Desktop.
Save jimfleming/5855904 to your computer and use it in GitHub Desktop.
Sizes a bounding box to its children.
BoxCollider collider = gameObject.AddComponent<BoxCollider>();
Bounds parentBounds = new Bounds(Vector3.zero, Vector3.zero);
Bounds childBounds = new Bounds(Vector3.zero, Vector3.zero);
MeshFilter[] meshFilters = transform.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter meshFilter in meshFilters) {
childBounds.center = transform.InverseTransformPoint(meshFilter.renderer.bounds.center);
childBounds.size = meshFilter.sharedMesh.bounds.size;
parentBounds.Encapsulate(temp);
}
collider.size = bounds.size;
collider.center = bounds.center;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment