render bounding box of the mesh or collider that is attached to the mesh?

i want to figure out how to draw the bounding box of the mesh or collider that is attached to the mesh.
i dont know what to use, bounding box of the mesh, collider or renderer? my goal is to get the position of all
vertices making the bounding box and then render the lines between so that i can get wireframe cube, this way
it will be easy to determine what object is selected in the scene.

is there a easy way to do this? and how to get the vertices. i can get the center of the bounding box like this:

Debug.Log(transformTemp.collider.bounds.center.ToString());

thanks!

Which bounds variable you use depends on which bounding box you want to display. The collider and renderer bounds give a box positioned around the object which is axis-aligned in world space (ie, it is always aligned with the world axes regardless of the rotation of the object. The mesh’s bounds are given in the object’s local coordinate space, but you can use transform.TransformPoint on the corner points of the box to convert them to world space (so the box will rotate to stay aligned with the object’s local axes).

hi andeeee, thanks for looking into this… here is what i tried in meanwhile

i decided to use renderer.bounds. i would normally use collider.bounds but some object have box some have mesh colliders, i am not does it matter. and i dont know are the renderer.bounds and collider.bounds the same?

i calculated 8 vertices like this:

//CURRENT TARGET TRANSFORM
	var ctt : Transform=ReturnCameraTargetTransform();
	
	//POSITIVE Z
	//1,1,1
v1=new Vector3(ctt.renderer.bounds.size.x+ctt.renderer.bounds.extents.x, ctt.renderer.bounds.size.y+ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z+ctt.renderer.bounds.extents.z);

	//-1,1,1
v2=new Vector3(ctt.renderer.bounds.size.x-ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y+ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z+ctt.renderer.bounds.extents.z);
							
	//-1,1,1
v3=new Vector3(ctt.renderer.bounds.size.x-ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y-ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z+ctt.renderer.bounds.extents.z);


	//1,-1,1
v4=new Vector3(ctt.renderer.bounds.size.x+ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y-ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z+ctt.renderer.bounds.extents.z);
				
	//NEGATIVE Z
	//1,1,-1
v5=new Vector3(ctt.renderer.bounds.size.x+ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y+ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z-ctt.renderer.bounds.extents.z);

	//-1,1,-1
v6=new Vector3(ctt.renderer.bounds.size.x-ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y+ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z-ctt.renderer.bounds.extents.z);
							
	//-1,-1,-1
v7=new Vector3(ctt.renderer.bounds.size.x-ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y-ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z-ctt.renderer.bounds.extents.z);


	//1,-1,-1
v8=new Vector3(ctt.renderer.bounds.size.x+ctt.renderer.bounds.extents.x,
ctt.renderer.bounds.size.y-ctt.renderer.bounds.extents.y,
ctt.renderer.bounds.size.z-ctt.renderer.bounds.extents.z);

then i rendered lines in this sequence v1-v2.v2-v3.v3-v4.v4-v1,v5-v6.v6-v7.v7-v8,v8-v1
this does not render the whole cube, it renderes only front and back rect. but it does not render it
where it should. i thought that problem lies in world and local space so i did this:

	v1=transform.TransformPoint(v1);
			v2=transform.TransformPoint(v2);
			v3=transform.TransformPoint(v3);
			v4=transform.TransformPoint(v4);
			v5=transform.TransformPoint(v5);
			v6=transform.TransformPoint(v6);
			v7=transform.TransformPoint(v7);
			v8=transform.TransformPoint(v8);

it didnt fix the problem, it renders the lines but again somewhere different than around the selected object.
so what is your opinion, where the problem is?

renderer.bounds is already in world space, so TransformPoint is not necessary if you use that one. However, I’d recommend using your current code with mesh.bounds instead, as it will be far more accurate than renderer.bounds (especially for rotated oblong objects).

the above code does not render bounding box around the selected object, it is placed outside…so i dont know what is wrong with code.

the reason i didnt use mesh.bounds is that i didnt know how to get the mesh of the selected object.
i just can figure out the code on the forums regarding this and mesh filter, components. it seems a lot easier
to get collider or renderer bounds instead. if you could explain how to do it i would appreciate it. thanks

The reason your code isn’t working is because you’re using renderer.bounds. Since renderer.bounds is already defined in world space, using transform.TransformPoint is incorrect and will place the box somewhere completely wrong. You could fix it by removing the transform.TransformPoint calls.

However, my suggestion is to use the mesh bounds instead. Mesh bounds are defined in local space and to use it you need to keep the TransformPoint calls. To obtain the mesh bounds of your ctt object, you can add this line:

var meshBounds = ctt.GetComponent<MeshFilter>().sharedMesh.bounds;

Then use meshBounds instead of your ctt.renderer.bounds everywhere.

here is what i did and what are the results. i followed your advice and used meshfilter

//CURRENT TARGET TRANSFORM
	var ctt : Transform=ReturnCameraTargetTransform();
	var meshBounds = ctt.GetComponent(MeshFilter).sharedMesh.bounds;
	
	
	//POSITIVE Z
	//1,1,1
	v1=new Vector3(meshBounds.size.x+meshBounds.extents.x,
							meshBounds.size.y+meshBounds.extents.y,
							meshBounds.size.z+meshBounds.extents.z);

	//-1,1,1
	v2=new Vector3(meshBounds.size.x-meshBounds.extents.x,
							meshBounds.size.y+meshBounds.extents.y,
							meshBounds.size.z+meshBounds.extents.z);

	
	//-1,1,1
	v3=new Vector3(meshBounds.size.x-meshBounds.extents.x,
							meshBounds.size.y-meshBounds.extents.y,
							meshBounds.size.z+meshBounds.extents.z);


	//1,-1,1
	v4=new Vector3(meshBounds.size.x+meshBounds.extents.x,
							meshBounds.size.y-meshBounds.extents.y,
							meshBounds.size.z+meshBounds.extents.z);
	
	//NEGATIVE Z
	//1,1,-1
	v5=new Vector3(meshBounds.size.x+meshBounds.extents.x,
							meshBounds.size.y+meshBounds.extents.y,
							meshBounds.size.z-meshBounds.extents.z);

	//-1,1,-1
	v6=new Vector3(meshBounds.size.x-meshBounds.extents.x,
							meshBounds.size.y+meshBounds.extents.y,
							meshBounds.size.z-meshBounds.extents.z);
							
	//-1,-1,-1
	v7=new Vector3(meshBounds.size.x-meshBounds.extents.x,
							meshBounds.size.y-meshBounds.extents.y,
							meshBounds.size.z-meshBounds.extents.z);


	//1,-1,-1
	v8=new Vector3(meshBounds.size.x+meshBounds.extents.x,
							meshBounds.size.y-meshBounds.extents.y,
							meshBounds.size.z-meshBounds.extents.z);

i got the same problem. here is what is going on, maybe it will help you diagnose the problem.
-bounding box where it should be
-it is much smaller now (i know i scaled the model when it was imported, maybe that is the cause)
-i made few simple cubes and tried this on them, i attached the results, the bounding box isnt where it should
be altough the size is ok, on other two boxes i get this bounding box?! it does not move, so same position and scale
for three cubes. this is strange.

let me know if you see what the problem is. thanks!

Ah, you are using meshBounds.size.x + meshBounds.extents.x. It should be meshBounds.center.x + meshBounds.extents.x. So replace all the meshBounds.size by meshBounds.center.

thanks again for looking into this, but once again no luck, here is what i did

//CURRENT TARGET TRANSFORM
	var ctt : Transform=ReturnCameraTargetTransform();
	var meshBounds = ctt.GetComponent(MeshFilter).sharedMesh.bounds;
	
	
	//POSITIVE Z
	//1,1,1
	v1=new Vector3(meshBounds.center.x+meshBounds.extents.x,
							meshBounds.center.y+meshBounds.extents.y,
							meshBounds.center.z+meshBounds.extents.z);

	//-1,1,1
	v2=new Vector3(meshBounds.center.x-meshBounds.extents.x,
							meshBounds.center.y+meshBounds.extents.y,
							meshBounds.center.z+meshBounds.extents.z);

	
	//-1,1,1
	v3=new Vector3(meshBounds.center.x-meshBounds.extents.x,
							meshBounds.center.y-meshBounds.extents.y,
							meshBounds.center.z+meshBounds.extents.z);


	//1,-1,1
	v4=new Vector3(meshBounds.center.x+meshBounds.extents.x,
							meshBounds.center.y-meshBounds.extents.y,
							meshBounds.center.z+meshBounds.extents.z);
	
	//NEGATIVE Z
	//1,1,-1
	v5=new Vector3(meshBounds.center.x+meshBounds.extents.x,
							meshBounds.center.y+meshBounds.extents.y,
							meshBounds.center.z-meshBounds.extents.z);

	//-1,1,-1
	v6=new Vector3(meshBounds.center.x-meshBounds.extents.x,
							meshBounds.center.y+meshBounds.extents.y,
							meshBounds.center.z-meshBounds.extents.z);
							
	//-1,-1,-1
	v7=new Vector3(meshBounds.center.x-meshBounds.extents.x,
							meshBounds.center.y-meshBounds.extents.y,
							meshBounds.center.z-meshBounds.extents.z);


	//1,-1,-1
	v8=new Vector3(meshBounds.center.x+meshBounds.extents.x,
							meshBounds.center.y-meshBounds.extents.y,
							meshBounds.center.z-meshBounds.extents.z);

now the bounding box appears in the center for all objects, also i noticed that if i scaled the box the bounding box will appear bigger and same for all boxes…

this all seems so confusing…and hard to get to work…

i even tried this:

v1=(meshBounds.center+meshBounds.extents);

	//-1,1,1
	v2=(meshBounds.center + Vector3.Scale(meshBounds.extents, Vector3(-1, 1, 1)));

	
	//-1,1,1
	v3=(meshBounds.center + Vector3.Scale(meshBounds.extents, Vector3(-1, 1, 1)));


	//1,-1,1
	v4=(meshBounds.center + Vector3.Scale(meshBounds.extents, Vector3(1, -1, 1)));

but nothing seems to place the bounding box in the center of the cube along with its appropriate size…
what is going on?

I think it is because you use transform.TransformPoint. transform is the Transform your script is on. However, you want to convert the bounds from their own transform. Try replacing transform.TransformPoint by ctt.TransformPoint.

oh yeah, that is correct my friend!
thanks a lot!