• 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 andycodes · Dec 02, 2016 at 05:11 AM · rotationboundsboundingbox

"Recalculate" the mesh bounds to the objects rotation?

I have played around with mesh.bounds, renderer.bounds, and collider.bounds but none of them really do what i want them to do. the mesh bounds is accurate to the objects bounding box but only at rotation 0, however this is only in local space and not the world space that i want.

The renderer and collider bounds are the axis aligned bounding boxes so when an object is rotated, the "bounding-box's bounding-box" per-se is taken. this results in bounding boxes that are deceiving when an object is rotated and doesn't represent the apparent size of the object.

Is there any way to get, or at least imitate, a bounding box but where the box is essentially the smallest AABB box that can contain the object with its current rotation?

Comment
f3flight
unity_NfOfiXnvG-rECg

People who like this

2 Show 1
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 Griffo · Dec 02, 2016 at 08:25 AM 0
Share

I'd like to know this too, as I've been struggling with Bounds .. Here

4 Replies

  • Sort: 
avatar image

Answer by tomekkie2 · Dec 02, 2016 at 09:31 AM

If you would't care about the performance, you could iterate over all the mesh vertices contained in the GameObject, including their position into the bounds.

 Bounds bounds;
 MeshFilter[] meshes = GetComponentsInChildren<MeshFilter>();
             for(int i = 0; i < meshes.Length; i++) {
                 Mesh ms = meshes[i].mesh;
                 int vc = ms.vertexCount;
                 for(int j = 0; j < vc; j++) {
                     if(i==0&&j==0){
                         bounds = new Bounds(ms.transform.TransformPoint(vertices[j]), Vector3.zero);
                     }else{
                         bounds.Encapsulate(ms.transform.TransformPoint(vertices[j]);
                     }
                 }
             }



Comment
Bunny83
f3flight

People who like this

2 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 Bunny83 · Dec 02, 2016 at 10:32 AM 1
Share

Note that there are cases where you can't use lossyScale like that. A mesh could have been sheard due to non-uniform scaling and a parent rotation. lossyScale (as teh name suggests) is only an approximation of the world space scale and you shouldn't rely on it. Also meshes[i].gameObject.transform is redundant, simply use meshes[i].transform.

avatar image tomekkie2 Bunny83 · Dec 02, 2016 at 01:08 PM 0
Share

Thanks for pointing that out. I have updated the above script.

avatar image

Answer by Bunny83 · Dec 02, 2016 at 10:27 AM

This question has been asked several times in the past and i wrote already several answers like this one.

ps: If you have several meshes in a rigid configuration (that only moves / rotates as a whole) or a single very complex mesh you might want to pre-calculate the convex hull-points of the mesh / meshes and only calculate the bounds on those vertices. This would reduce / simplify the per-frame overhead.

Comment

People who like this

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

Answer by CiberX15 · Apr 16, 2019 at 01:05 AM

What I ended up doing to solve this problem for my project was to have a function that stored the target object's rotation, then zeroed it, grabbed the meshs's bounding box while rotation was zero, then restored the rotation.

It's a kind of hacky solution, and I have no idea what it would do if the target was a physics object, but it worked for my needs.

Other potential work around might be adding a box collider then getting the bounds from that, then removing the box collider, though that might come with a greater performance hitch.

Comment

People who like this

0 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 Bunny83 · Apr 16, 2019 at 09:35 AM 0
Share

This makes no sense. You said you grabbed the mesh's bounding box. The "mesh.bounds" value doesn't change when the object rotates since the bounds are given in local space coordinates. Renderer.bounds and Collider.bounds on the other hand do change when the object is rotated. However if the object is rotated to (0,0,0) it's actually the same size as mesh.bounds, just that mesh.bounds has it's center defined in localspace and renderer.bounds in worldspace.

avatar image

Answer by iwanPlays · Apr 01 at 08:46 PM

I ended up here not due to rotation but due to false mesh editing but in my case, simply meshFilter.sharedMesh.RecalculateBounds() fixes it

Comment

People who like this

0 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Size of collider bounding box regardless of rotation 1 Answer

Rotating Raycast with object?? 1 Answer

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

Why isn't Bounds.Encapsulate working as expected? 1 Answer

Is the gameobject is on the same level with it's surface on which it is located? 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