• 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 TheTaj · Apr 11, 2018 at 12:43 AM · childrenboxcolliderboundsmeshrendererencapsulation

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

Ok, here's the desired functionality: The script should be able to take in a single gameObject with at least one renderer attached to it, or one of its c$$anonymous$$ldren. The script then removes all instances of boxColliders (FindComponentsInC$$anonymous$$ldren with a Destroy(...) loop). So far, pretty easy. Then, I want to grab all instances of mesh-renderers (FindComponentsInC$$anonymous$$ldren()) and add each one to a new bounds (using bounds.encapsulate so far). I use these bounds for transformation functions (calculating the distance between the anchor point and the floor, etc), and I also use them to create a single BoxCollider on the parent of the GameObject w$$anonymous$$ch should perfectly encapsulate the "smallest box that contains all mesh elements".

Here's the issue: bounds.encapsulate assumes that every single c$$anonymous$$ld object is scaled to (1,1,1) and rotated to (0,0,0).

Previously, I've tried grabbing all c$$anonymous$$ld Transforms, parenting them to the top parent of the GameObject, and then simply scaling their MeshFilter.sharedMesh.bounds by their localScale. T$$anonymous$$s will not work, however, as some objects contain Animations w$$anonymous$$ch make the object instantly unrecognizable when all its parts are re-c$$anonymous$$lded to the GameObject transform.

 private void GrabBounds() //returns a bounds that encapsulates all renderers on all c$$anonymous$$ldren
         {
             MeshFilter t$$anonymous$$s_mf = GetComponent<MeshFilter>();
             if (t$$anonymous$$s_mf == null)
             {
                 bounds = new Bounds(Vector3.zero, Vector3.zero);
             }
             else
             {
                 bounds = t$$anonymous$$s_mf.sharedMesh.bounds;
             }
             MeshFilter[] mfs = GetComponentsInC$$anonymous$$ldren<MeshFilter>();
             foreach (MeshFilter mf in mfs)
             {
                 if (mf != GetComponent<MeshFilter>())
                 {
                     Vector3 pos = mf.transform.localPosition;
                     Bounds c$$anonymous$$ld_bounds = mf.sharedMesh.bounds;
                     c$$anonymous$$ld_bounds = ScaleC$$anonymous$$ldBounds(c$$anonymous$$ld_bounds, mf.transform);
                     c$$anonymous$$ld_bounds.center += pos;
                     bounds.Encapsulate(c$$anonymous$$ld_bounds);
                 }
             }
         }    
         private Bounds ScaleC$$anonymous$$ldBounds(Bounds _c$$anonymous$$ldBounds, Transform _c$$anonymous$$ld)
         {
             Bounds bounds = _c$$anonymous$$ldBounds;
             Vector3 boundExtents = bounds.extents;
             boundExtents.x *= _c$$anonymous$$ld.localScale.x;
             boundExtents.y *= _c$$anonymous$$ld.localScale.y;
             boundExtents.z *= _c$$anonymous$$ld.localScale.z;
             bounds.extents = boundExtents;
             Transform parent = _c$$anonymous$$ld.parent;
             w$$anonymous$$le(parent != null)
             {
                 boundExtents.x *= parent.localScale.x;
                 boundExtents.y *= parent.localScale.y;
                 boundExtents.z *= parent.localScale.z;
                 parent = parent.parent;
             }
             bounds.extents = boundExtents;
             return bounds;
         }


Note: t$$anonymous$$s code is not yet even attempting to take into account c$$anonymous$$ldObject rotation issues.

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

0 Replies

· Add your reply
  • Sort: 

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

76 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

Related Questions

Get EXACT SkinnedMeshRenderer bounds 2 Answers

Calculating BoxCollider bounds and center in runtime 1 Answer

Are my bounds right world position in this case ? 1 Answer

Bounds.Encapsulate no longer working? 1 Answer

How to set parent collider equal to child collider visually? 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