Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
Add comment · 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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

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

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

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

72 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

Related Questions

Size of collider bounding box regardless of rotation 1 Answer

Skinned Mesh Renderers Bounds rotated incorrectly when importing from Maya. 0 Answers

Is the bounds.size and the GetComponent.BoxCollider2D.size of a GameObject meant to be the same? 1 Answer

Flip over an object (smooth transition) 3 Answers

How to project the position of a point relative to a rotated collider using its bounds? 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