i am trying to get the center of the group of objects, so that i can center the camera to that group and have all subobjects
in the screen.
here is the code i am using:
var bound : Bounds= new Bounds(Vector3.zero,Vector3.zero);
var Colliders = target.GetComponentsInChildren(Collider);
for(var collider : Collider in Colliders){
if(collider.tag!="group" || collider.tag!="microlabel"){
bound.Encapsulate(collider.bounds);
}
}
Debug.Log("center: " + bound.center.ToString() + " size: " + bound.size.ToString());
var sphere : GameObject=GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position=bound.center;
sphere.transform.localScale=Vector3(0.2,0.2,0.2);
this works fine for group of groups of objects, but it does not calculate the group that only holds objects
i have offset by the y axis, what to do? maybe i am missing something? how to calculate this type of thing
properly?
ok i guess that i should use the mesh.bounds instead of collider bounds. it is harder to get those i think. can somebody help me replace collider bounds with mesh.bounds in the code i provided. any help appreciated!
using renderer.bounds instead of collider.bounds gives same result
also when i click on the group of object in the hierarchy i gizmo positions correctly in the center of the objects
it gives me 0,0,0 for position no matter what group i choose?!
//BOUNDS ARE CREATED WITH CENTER AND SIZE
var bound : Bounds= new Bounds(Vector3.zero,Vector3.zero);
var arr =target.GetComponentsInChildren(Transform);
var children = new Array();
for(i=0;i<arr.length;i++){
if(!(arr[i].tag=="group" || arr[i].tag=="microlabel")){
children.Push(arr[i]);
}
}
for(i=0;i<children.length;i++){
bound.Encapsulate(children[i].GetComponent(MeshFilter).mesh.bounds);
}
var filters = target.GetComponentsInChildren(MeshFilter);
for(var filter : MeshFilter in filters)
{
//if members are not tagged group or microlabel since these are just groups
if(filter.tag!="group" || filter.tag!="microlabel")
{
bound.Encapsulate(filter.mesh.bounds);
}
}
(thanks Ejlersen for this)
but
puting the sphere in the position of the bound does not give me a good result. it seems that all calculated bounds
are in the same position…
var sphere : GameObject=GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position=bound.center;
although they have different coords, i got them with this line:
Necro of a necro of a necro, but worth it IMO. This really is a great little piece of code. You could also adapt this to work with colliders a bit faster/more efficiently than renderer bounds, although you’d lose a lot of precision in the process.
I haven’t seen anything around, but part of me is curious if there’s a method in Unity to do exactly this, seeing as how useful it is and that it’s been years since this solution was presented.
public static Bounds GetBounds(GameObject obj)
{
Bounds bounds = new Bounds();
Renderer[ ] renderers = obj.GetComponentsInChildren<Renderer>();
if (renderers.Length > 0)
{
//Find first enabled renderer to start encapsulate from it
foreach (Renderer renderer in renderers)
{
if (renderer.enabled)
{
bounds = renderer.bounds;
break;
}
}
//Encapsulate for all renderers
foreach (Renderer renderer in renderers)
{
if (renderer.enabled)
{
bounds.Encapsulate(renderer.bounds);
}
}
}
return bounds;
}
Tried all of these and did not get a real bounds that wasn’t axis alligned so the easiest way to take each prefab and add my own “bounds” box that I manually scale. That will rotate with the object and be the correct real bounds that can be used for whatever you want (I wanted to hilight the object the user points to).