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 Saiaku · Mar 12, 2019 at 03:50 PM · collidersboxcollider

Resizing dynamically added BoxCollider to encapsulate all child objects' total bounds

I have a GameObject consisting of several children. Each child holds another set of children that contain actual meshes, but the general shape of the total object can be summed up to a box (think, chest body and lid with meshes for wood and decor).

I haven't been able to find a way to scale the boxcollider to represent the full size of the parent gameobject in world space. A lot of the answers I find solve similar issues with a Renderer component which seems deprecated.

What should I do here? (this must be added and scaled dynamically)

Comment
Add comment · Show 3
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 RobAnthem · Mar 12, 2019 at 03:56 PM 0
Share

Only thing I can think of is to iterate all child objects, pool them into a list of renderers, then iterate the Bounds of each renderer, test outside the bounds to find the farthest bound on each side, then resize your box to match the farther bounds of all objects.

avatar image Saiaku RobAnthem · Mar 12, 2019 at 03:58 PM 0
Share

Are you referring to their $$anonymous$$eshRenderer?

avatar image RobAnthem Saiaku · Mar 12, 2019 at 04:02 PM 0
Share

That is one type of renderer. All Renderers have a Bounds property. You can generically get any type of renderer the object contains by doing

 Renderer rend = GetComponent<Renderer>();




Basically to get your bounds of the final box, you would just compare 6 floats, $$anonymous$$X, maxX, $$anonymous$$Y, maxY, $$anonymous$$Z, maxZ. Test every renderers bounds to find the largest and smallest numbers of each, and then you have your final bounds. To be honest, the quickest way might be to take each renderer pisition and move out by Vector3.up someDistance and Vector3.Right someDistance etc, that way you can call the Bounds.ClosestPoint function to get the closest point after your outside the bounds, this should give you the farthest outside point of each vector. You'll just need to iterate ClosestBounds 6 times per renderer to get forward, back, up, down, left, and right.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Saiaku · Mar 12, 2019 at 05:55 PM

Thanks to @RobAnthem who led me onto the right tracks. :) Here is the solution:

The parent sets up the children by calling something like

 SetCollidersFrom<BoxCollider>(m_Body, m_Rotater.GetContainer());

which does

     public void SetCollidersFrom<CollType>(params GameObject[] from) where CollType : Collider
     {
         if (!m_UseCollider) { return; }
         if (from == null)   { Debug.LogWarning("<color=" + DebugColor + ">" + DebugName + "SetCollidersFrom -> Invalid parameters.</color>"); return; }
 
         int added = 0;
         List<MeshRenderer>  meshrend        = new List<MeshRenderer>();
         List<Bounds>        boundsForEach   = new List<Bounds>();
         Bounds              totalBounds     = new Bounds();
         int                 index           = 0;
 
         foreach (GameObject go in from)
         {
             if (!BlacklistedNames.Any(go.name.Contains))
             {
                 var bounds = _GetChildRendererBounds(go);
                 boundsForEach.Add(bounds);
                 totalBounds.Encapsulate(bounds);
             }
         }
 
         foreach(GameObject go in from)
         {
             if (typeof(CollType) == typeof(BoxCollider))
             {
                 BoxCollider bc = go.AddComponent<BoxCollider>();
                 bc.enabled = true;
                 bc.size = (boundsForEach[index].size * 2) * m_ColliderPadding;
                 bc.center = new Vector3(0f, (bc.size.y / 2), 0f);
                 added++;
                 index++;
             }
         }
 
         if (added > 0)
         {
             Debug.Log("<color=" + DebugColor + ">" + DebugName + added.ToString() + " colliders set.</color>");
         }
     }

which uses helper function:

     private Bounds _GetChildRendererBounds(GameObject go)
     {
         var renderers = go.GetComponentsInChildren<Renderer>();
 
         if (renderers.Length > 0)
         {
             Bounds bounds = renderers[0].bounds;
             foreach (var rend in renderers)
             {
                 if (!BlacklistedNames.Any(rend.gameObject.name.Contains))
                 {
                     bounds.Encapsulate(rend.bounds);
                 }
             }
             return bounds;
         }
         else { return new Bounds(); }
     }

And the result: alt text


colliders.png (144.6 kB)
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

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

102 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

Related Questions

horse with rigid body and boxcollider wont collide with anything i tried mesh collider with and without convex , i tried box collider with and without rigid body. 2 Answers

How come FPS charcter ignores any possible collider? 3 Answers

Calculate Distance between 2 colliders 2 Answers

i wanna prevent my collider from falling down flat when climbing slope 0 Answers

Hey guys, can you help me out with my colliders. 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