• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pretender · Oct 11, 2010 at 12:41 PM · renderboundswireframeboundingbox

render the bounding box of collider or mesh how to?

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by QuantumRyan · Jun 29, 2011 at 09:09 PM

I do something like this in a script attached to the GameObject. It allows me to use the in scene Gizmo's toggle, as well as my own selection options (like mouse click). (This only applies in the unity Game and Scene windows FYI.)

   public bool _bIsSelected = true;
 
   void OnDrawGizmos()
   {
     if (_bIsSelected)
       OnDrawGizmosSelected();
   }
 
 
   void OnDrawGizmosSelected()
   {
     Gizmos.color = Color.yellow;
     Gizmos.DrawSphere(transform.position, 0.1f);  //center sphere
     if (transform.renderer != null)
       Gizmos.DrawWireCube(transform.position, transform.renderer.bounds.size);
   }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HiddenMonk · Feb 26, 2015 at 11:16 PM 0
Share

The above script works fine, but I noticed more accurate results when ins$$anonymous$$d of using transform.position in "Gizmos.DrawWireCube(transform.position, transform.renderer.bounds.size);" I use transform.renderer.bounds.center

The documentation even says renderer.bounds.center is more precise http://docs.unity3d.com/ScriptReference/Renderer-bounds.html

edit - although using renderer.bounds.center do accurately show the bounding box covering all of the mesh, When doing calculations in regard to the collider, then using the renderer wont be accurate. So Ins$$anonymous$$d use collider.bounds.center and collider.bounds.size

(side note - I don't really like how every little edit I do bumps the thread in unity answers)

avatar image
0

Answer by ga5p0d3 · Nov 19, 2019 at 10:05 AM

Here's my version, which ensures all (self and) child meshes have their bounds drawn and the full object/sub-object transform is used to draw each bounding box.

 void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.magenta;
     foreach(var mf in GetComponentsInChildren<MeshFilter>())
     {
         Gizmos.matrix = mf.transform.localToWorldMatrix;
         Mesh m = mf.sharedMesh;
         Gizmos.DrawWireCube( m.bounds.center, m.bounds.size );
     }
 }





Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get the bounds of an actual sprite, not it's bounding box. 1 Answer

(GameObject).renderer.bounds.size.(axis) returns 0. 0 Answers

Instantiate position tracking? 1 Answer

"Recalculate" the mesh bounds to the objects rotation? 3 Answers

Reporting the vertices of a 3D Bounding Box to Screen position 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges