• 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
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

People who like this

0 Show 0
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

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
HiddenMonk

People who like this

1 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 instead 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 Instead 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

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
SunnySunshine

People who like this

1 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Issue with checking to see whether the Bounds value's of two game objects equal each other 1 Answer

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

Skinned Mesh disappears if the camera is too close. 2 Answers

Instantiate position tracking? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges