Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by pretender · Oct 11, 2010 at 01:26 PM · boundingbox

how to get 8 vertices from bounds properties?

i want to render boundingbox of the collider and i know about properties of bounds like size,extents and center but i dont know how to get 8 vertices that make bounding box.

any help here from somebody experienced?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by MrSkiz · Jun 05, 2013 at 11:43 AM

Hi,

I know this topic is pretty old but here is my take on how to get the 8 points of a bounding cube : just use the Bounds.min and Bounds.max variables to get the two extreme points, then alternate between min.x/y/z and max.x/y/z to construct the 6 other Vector3.

     boundPoint1 = collider.bounds.min;
     boundPoint2 = collider.bounds.max;
     boundPoint3 = Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z);
     boundPoint4 = Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z);
     boundPoint5 = Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z);
     boundPoint6 = Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z);
     boundPoint7 = Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z);
     boundPoint8 = Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z);

Easy, quite typo-free.

Comment
Add comment · Show 2 · 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 awake · Jan 20, 2014 at 08:14 PM 0
Share
 //if you want to visualize the box you can add to $$anonymous$$rSkiz's code:
         Color lineColor = Color.green;
         // rectangular cuboid
         // top of rectangular cuboid (6-2-8-4)
         Debug.DrawLine (boundPoint6, boundPoint2, lineColor);
         Debug.DrawLine (boundPoint2, boundPoint8, lineColor);
         Debug.DrawLine (boundPoint8, boundPoint4, lineColor);
         Debug.DrawLine (boundPoint4, boundPoint6, lineColor);
 
         // bottom of rectangular cuboid (3-7-5-1)
         Debug.DrawLine (boundPoint3, boundPoint7, lineColor);
         Debug.DrawLine (boundPoint7, boundPoint5, lineColor);
         Debug.DrawLine (boundPoint5, boundPoint1, lineColor);
         Debug.DrawLine (boundPoint1, boundPoint3, lineColor);
 
         // legs (6-3, 2-7, 8-5, 4-1)
         Debug.DrawLine (boundPoint6, boundPoint3, lineColor);
         Debug.DrawLine (boundPoint2, boundPoint7, lineColor);
         Debug.DrawLine (boundPoint8, boundPoint5, lineColor);
         Debug.DrawLine (boundPoint4, boundPoint1, lineColor);
avatar image hoodymong · Jan 05, 2017 at 10:06 AM 0
Share
         // if you want make a cube
                 var bounds$$anonymous$$esh = go.AddComponent<$$anonymous$$eshFilter>(); ;
         bounds$$anonymous$$esh.mesh = new $$anonymous$$esh();
         bounds$$anonymous$$esh.mesh.vertices = new [] { boundPoint1, boundPoint2, boundPoint3, boundPoint4, boundPoint5, boundPoint6, boundPoint7, boundPoint8 };
         bounds$$anonymous$$esh.mesh.triangles = new[]
         {
             0,7,4,
             0,3,7,
             5,1,3,
             3,1,7,
             7,1,4,
             4,1,6,
             5,3,2,
             2,3,0,
             0,4,2,
             2,4,6,
             1,5,2,
             6,1,2
         };
avatar image
9

Answer by Bampf · Oct 11, 2010 at 02:52 PM

First I'll answer the question, then I'll mention two alternatives that don't involve computing the 8 vertices.

1) The extents (x, y, and z) are half of the size in those directions. So you can get one corner by subtracting the extents from the center, and the opposite corner by adding the extents:

Vector pt1 =  collider.bounds.center - collider.bound.extents;
Vector pt2 =  collider.bounds.center + collider.bound.extents;

The other corners can be found by adding some extents, subtracting others. Something like:

Vector pt3 = New Vector3(collider.bounds.x - collider.bound.extents.x, 
                         collider.bounds.y + collider.bound.extents.y, 
                         collider.bounds.z + collider.bound.extents.z); 

This time I subtracted x, added y, added z. There are 8 possible combinations of additions and subtractions. Those are your 8 vertices.

2) But maybe you don't need the 8 vertices. You could instead create a GameObject that's a 1x1x1 cube, then move it to the center of the collision, and set the scale of the cube to match the size of the collider. I didn't test this, but it would look something like this:

GameObject myCube = GameObject.CreatePrimitive(PrimitiveType.Cube); // or, instantiate a prefab cube
myCube.tranform.position = collider.bounds.center;
myCube.tranform.localScale = collider.bounds.size;

(And instead of creating a primitive cube, you could instantiate a prefab cube, which would have whatever material, scripts, and other components you wanted to put on it.)

3) If you just want to draw it in the Scene View for debugging purposes, you can simply call Gizmos.DrawCube or Gizmos.DrawWireCube, passing it bounds.center and bounds.size as the arguments.

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
avatar image
-1

Answer by hoodymong · Jan 05, 2017 at 10:19 AM

// if you want make a cube mesh

            var boundsMesh = go.AddComponent<MeshFilter>();
     boundsMesh.mesh = new Mesh();
         boundsMesh.mesh.vertices = new [] { boundPoint1, boundPoint2, boundPoint3, boundPoint4, boundPoint5, boundPoint6, boundPoint7, boundPoint8 };
         boundsMesh.mesh.triangles = new[]
         {
             0,7,4,
             0,3,7,
             5,1,3,
             3,1,7,
             7,1,4,
             4,1,6,
             5,3,2,
             2,3,0,
             0,4,2,
             2,4,6,
             1,5,2,
             6,1,2
         };
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Click and Drag - How to? 2 Answers

LodGroup manually set Bounds 0 Answers

Unity's Bounding Box 0 Answers

2D bounding box in screen space from a 3D object 0 Answers

What exactly is the bounding box? 0 Answers


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