Hello,
did anyone have an Idea how to do this?
I’ve objects in target but howto draw an bounding box around the target? Hints are Welcome ![]()
Hello,
did anyone have an Idea how to do this?
I’ve objects in target but howto draw an bounding box around the target? Hints are Welcome ![]()
1 if you want to draw normal bounding boxes just hit the play button and select the target object and then enable drawing gizmos in gameview (don’t maximize game view).
2 you can use line renderer to draw lines and getting bounding boxes is easy
just use
Collider.bounds and properties of bounds class to get those min and max points and draw a cube easily
hope this helps
Ok I get Bounds.center and Bounds.extents now, and how can I use it together with the Line Renderer to draw a bounding box?
add a line renderer component to your gameobject and set it’s “use worldspace” to true. set vertexCount to 8 and then use the values you get from bounds for 8 vertecies of the cube
i am interested how this could be done, can somebody give detailed explanation.
i know about size,extens and other bounds properties but how to get the 8 vertices that make bounding box
Attached is a script that shows how to get the corners of the object’s bounding box. The key is to use the extents variable. This is a Vector3 that is set to half the width of the box in each dimension (so the X component is equal to half the width in the local X axis, etc). However, another way to interpret it is as a vector pointing from the centre of the box to the top front right corner. Therefore, if you add the extents vector to the centre point, you get the coordinate of the top front right corner. You can then negate the axes of the extents in different combinations to get all the corners (for example, if you negate the Y axis of top-front-right, you get bottom-front-right, etc). The mesh bounds are given in the object’s local space, but it is easy to convert them to world space with transform.TransformPoint in order to draw the box with Debug.DrawLine or whatever.
405416–13950–$CalcBounds.js (1.95 KB)
To anyone who ends up on here from google, QuantumRyan’s answer over at the answers.unity3d, is nicely succinct and does the job in fewer lines.
And will only work in the editor. also nice necro.
To the OP I cheat when I do this. I use the bounds.center and bounds.size to scale a prefab cube that’s textured transparently in the middle but solid along the 4 edges to create a line like effect.
I have just submitted some assets on this to the AssetStore.
There is a free asset on the runtime drawing bounding box:
and a commercial asset on the same, but including dimensions: