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
0
Question by TheTaj · Apr 11, 2018 at 12:43 AM · childrenboundsboxcollidermeshrendererencapsulation

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 children. The script then removes all instances of boxColliders (FindComponentsInChildren with a Destroy(...) loop). So far, pretty easy. Then, I want to grab all instances of mesh-renderers (FindComponentsInChildren()) 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 which should perfectly encapsulate the "smallest box that contains all mesh elements".

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

Previously, I've tried grabbing all child Transforms, parenting them to the top parent of the GameObject, and then simply scaling their MeshFilter.sharedMesh.bounds by their localScale. This will not work, however, as some objects contain Animations which make the object instantly unrecognizable when all its parts are re-childed to the GameObject transform.

 private void GrabBounds() //returns a bounds that encapsulates all renderers on all children
         {
             MeshFilter this_mf = GetComponent<MeshFilter>();
             if (this_mf == null)
             {
                 bounds = new Bounds(Vector3.zero, Vector3.zero);
             }
             else
             {
                 bounds = this_mf.sharedMesh.bounds;
             }
             MeshFilter[] mfs = GetComponentsInChildren<MeshFilter>();
             foreach (MeshFilter mf in mfs)
             {
                 if (mf != GetComponent<MeshFilter>())
                 {
                     Vector3 pos = mf.transform.localPosition;
                     Bounds child_bounds = mf.sharedMesh.bounds;
                     child_bounds = ScaleChildBounds(child_bounds, mf.transform);
                     child_bounds.center += pos;
                     bounds.Encapsulate(child_bounds);
                 }
             }
         }    
         private Bounds ScaleChildBounds(Bounds _childBounds, Transform _child)
         {
             Bounds bounds = _childBounds;
             Vector3 boundExtents = bounds.extents;
             boundExtents.x *= _child.localScale.x;
             boundExtents.y *= _child.localScale.y;
             boundExtents.z *= _child.localScale.z;
             bounds.extents = boundExtents;
             Transform parent = _child.parent;
             while(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: this code is not yet even attempting to take into account childObject rotation issues.

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

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.

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

Calculating BoxCollider bounds and center in runtime 1 Answer

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

Get EXACT SkinnedMeshRenderer bounds 2 Answers

Check if the bounds of the Cube (Unity Primitive) is within EmptyGameObjectWithBoxCollider bounds 1 Answer

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