- Home /
Draw the bounding box for a selected game object, outside of editor?
The idea here is to draw a bounding box to indicate to the user that a game-object is selected. How can I do this outside of Editor mode (say published player)?
Note: What if I wish the bounding box to be rendered within its 3d space
Answer by Eric5h5 · Jun 11, 2010 at 08:14 AM
If by "3D bounding box" you mean something like a cube, then do this: Instantiate a cube with a partially-transparent material, give it the same position and rotation of the object, then use the object's mesh.bounds to scale the cube to the correct size.
If you're talking about wireframes, then yeah, you can certainly make 3D wireframe bounding boxes with Vectrosity. Unfortunately wireframe line drawing is normally not that simple to do in Unity outside the editor, which is why I made this utility, in order to fix that problem.
Answer by newbrand · Jun 10, 2010 at 02:32 PM
(Assuming that you mean a bounding box as in most rts-games and not a 3d bounding box:)
I'm treading on very thin ice here because I never tinkered with bounds, so I can only guess how I would do it if I had to. Sorry if I state the obvious.
- The bounds of your mesh are stored in mesh.bounds: Bounds.
- Use Camera.WorldToScreenPoint to translate the min and max values of bounds to screen coordinates.
- Draw your box in OnGUI (just drawing the edges would probably be easier)
- Alternatively you could probably also instantiate a plane/cube with the proper texture applied and align/resize it to fit.
See also:
- http://unity3d.com/support/documentation/ScriptReference/Mesh-bounds.html and
- http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToScreenPoint.html
Answering your comment: See #4. If you use a transparent material with a border-texture it shouldn't look too bad as long as the box is not stretched too much. Alternatively, take a look at Eric5h5's Vectrosity if you have a few bucks to spend: http://forum.unity3d.com/viewtopic.php?t=53268
Meta: Seriously, why can't I post comments? I got enough of this stupid rep according to the faq, so what's the big deal?
Answer by tomekkie2 · Jul 06, 2011 at 02:04 PM
I have developed my own solution for a runtime wireframe bounding box. I have based it on the mesh.renderer.bounds or alternatively collider, dealt with a script attached to object. The box itself is being drawn by another script attached to camera and using GL.LINES. You can see in working on my blog page http://virtualplayground.d2.pl and can download these scripts from there.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Modifying PlayerSettings for RenderPath 1 Answer
Camera Meshrenderer in a multiplayer game 2 Answers
Gun clipping through walls in third person shooter 1 Answer
using camera.Render() to render to a RenderTexture is not depth testing? 0 Answers