• 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 ChristmasEve · Jan 11, 2019 at 02:31 AM · colliderboxcolliderboundsmeshrendererskinnedmeshrenderer

Get EXACT SkinnedMeshRenderer bounds

I have been trying to do this for a week. I've seen a lot of posts on the subject but none that have solved my problem. I need to get the smallest bounding box for a gameobject that has Skinned Mesh Renderers attached to it. They could have any rotation, scale or local position in the hierarchy. I need a routine that will bake the meshes into temp meshes and get the vertices and find a perfect box that isn't too big. I'm using it to make a special collider that only collides with a raycast from the screen. I have tried everything I could imagine, starting with the obvious solution and that is to just use the renderer's bounds. The last thing I tried was iterating all vertices and finding the min/max for X,Y and Z. This was the closest I've gotten. The size and orientation look pretty good but the center doesn't work for all of my monsters. Even the size can be too big but the position seems to be the biggest issue. How hard can it be to get that little box that Unity already shows in the editor when you pause playback?

Any help would be so greatly appreciated. You don't need to know anything about my game or what I'm doing specifically in order to help. I simply want to pass in a GameObject and get out a bounds object (two vectors) telling me the center and size for all Skinned Meshes within. A bonus would be if I could also include non-skinned meshes (like rigid weapons they may be holding) but I think that will be easier.

Comment
whydoidoit

People who like this

1 Show 4
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 ChristmasEve · Jan 24, 2019 at 03:48 PM 0
Share

Nobody's done this before? I still have no single solution that works for all 3D models, This is making the simple task of targeting monsters a nightmare in my game. I cannot believe a game engine like Unity doesn't have a built in method to give you the real bounds of something.

avatar image ChristmasEve ChristmasEve · Mar 03, 2019 at 02:39 AM 0
Share

I figured this out finally. I got it working perfectly. I can always get the exact bounds of any gameobject hierarchy, no matter what twisted mess of rotations and scaling the children have. Seems like this forum is dead though. If anyone is out there and you need the same thing, just ask me. I won't take the time to post anything.

avatar image DaveL99 ChristmasEve · Jun 25, 2019 at 10:46 AM 0
Share

I for one would be very grateful if you were able to post something about your solution. I'm sure it would be useful to others as well. Thanks!

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by DaveL99 · Oct 25, 2019 at 03:34 PM

Here is what I did:

I store a copy of the gameObject's position & rotation, and then set the position to zero and rotation to identity - effectively (temporarily) making local space and world space the same thing.

I then go through all Renderer objects that exist within the gameObject and it's children, and build up the AABB using Bounds.Encapsulate and the Renderer's .bounds property (which returns AA bounds accounting for animations etc. in World-space - but since I've put the object at the origin this is also Local-space).

Finally, I restore the original position & rotation on the gameObject's transform, and then from my "local-space" bounding-box I construct 8 corner vertices and then transform each one into World-space with transform.TransformPoint.

That gives me a non axis-aligned bounding box in world-space, that remains consistent regardless of how the object is rotated in the world etc.

One of the biggest gotchas that people might encounter, is that you can't simply initialise your bounds to zero, or empty. i.e. Don't do this:

       boundingBox = new Bounds ();
 
       for (int i = 0; i < renderers.Count; i++)
       {
         boundingBox.Encapsulate (renderers [i].bounds);
       }
 

Instead you should do this:

       boundingBox = renderers [0].bounds;
 
       for (int i = 1; i < renderers.Count; i++)
       {
         boundingBox.Encapsulate (renderers [i].bounds);
       }
 

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 punk · Aug 31, 2020 at 06:14 PM

Here's they way I got it working, just bear in mind that because the bounds are local just make sure your root bone is pointing up, otherwise the bounds will be at an angle

 skinnedMeshRenderer.updateWhenOffscreen = true;
 Bounds bounds = new Bounds();
 Vector3 center = skinnedMeshRenderer.localBounds.center;
 Vector3 extents = skinnedMeshRenderer.localBounds.extents;
 bounds.center = center;
 bounds.extents = extents;
 skinnedMeshRenderer.updateWhenOffscreen = false;
 skinnedMeshRenderer.localBounds = bounds;
Comment
Mortalanimal

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 Mortalanimal · May 28, 2022 at 07:47 PM 0
Share

First time I talk here in years, just to thank you!

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

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

Dynamically creating a box collider for an object with multiple, rotated, and scaled mesh renderers 0 Answers

Bounds and unwanted offset 0 Answers

How to set parent collider equal to child collider visually? 2 Answers

Mesh collider not working as expected 2 Answers

Bounds with Rotation: Getting y value of the point where its on the most left side of the collider (bounds.min.x) Note that the box collider has been rotated as well. 2 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