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
1
Question by Ryabkin · Feb 27, 2017 at 11:26 PM · unity 5childrenboundsboxcollider

Calculating BoxCollider bounds and center in runtime

alt text

I have a root prefab where I want to add box-collider in runtime based on bounds of child prefabs with letter meshes combined in a word. On the picture I changed size and center manually to match bounds of characters.

This is what I tried:

     Bounds bounds = new Bounds(parentObject.transform.position, Vector3.zero);
     foreach (MeshFilter meshFilter in parentObject.GetComponentsInChildren<MeshFilter>())
     {
         bounds.Encapsulate(meshFilter.mesh.bounds);
     }
     BoxCollider collider = parentObject.AddComponent<BoxCollider>();
     collider.center = bounds.center;
     collider.size   = bounds.size;

I'm getting enormous collider in this case. And it looks like somewhere wrong coordinates conversion.

How can I get size of meshes to calculate correctly offset for the next letter to add (currently I'm using hardcoded constant) ?

How can I calculate center and bounds for box collider in correct coordinate space ?

lol3.png (202.4 kB)
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 jamojamil · Feb 28, 2017 at 12:51 AM 0
Share

nice one :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 28, 2017 at 12:09 AM

Mesh.bounds is in local space of the mesh. That means no scaling is applied and no translation. So the location of each character is completely ignored. Are the single character objects scaled or rotated in some way? If the child objects are rotated you can't really use Mesh.bounds. If it is just scaled and translated, you have to scale and translate the bounds you get from your Mesh according to the transform of he child object,

So bounds.size need to be scaled by the childs localscale (You can use Vector3.Scale) and the center need to be offset by the localPosition of the child object.

edit
If the childs are rotated and you need an exact bounds, you may need to iterate through all vertices of each mesh, transform them into the local space of the parent (child-localspace --> worldspace --> parent-localspace) and use encapsulate with all vertex positions.

Comment
Add comment · Show 6 · 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 jamojamil · Feb 28, 2017 at 12:55 AM 0
Share

yes greate one :D

avatar image Ryabkin · Feb 28, 2017 at 06:49 AM 0
Share

Child prefabs have default transformation except X position. Scale and rotation applied to Root prefab only.

avatar image Ryabkin · Feb 28, 2017 at 08:48 PM 0
Share
 BoxCollider boxCollider = parentObject.GetComponent<BoxCollider>();
         Bounds       totalBound = new Bounds(Vector3.zero, Vector3.zero);
         $$anonymous$$eshRenderer mf = null;
         foreach ($$anonymous$$eshRenderer meshFilter in parentObject.GetComponentsInChildren< $$anonymous$$eshRenderer>())
         {
             float scale = mf.transform.localScale.x;
             Bounds meshBounds = meshFilter.bounds;
             totalBound.Encapsulate(new Bounds(meshBounds.center * scale, meshBounds.size * scale));
         }
         
         boxCollider.size   = totalBound.size;
         boxCollider.center = totalBound.center;
avatar image Ryabkin · Feb 28, 2017 at 08:49 PM 0
Share

To be more precise about prefabs position and scale I've added new screenshot. I tried to use scale but result is the same. Can you show me sample code ??

avatar image Bunny83 Ryabkin · Mar 01, 2017 at 12:10 AM 0
Share

Well, you forgot to add the position of each object:

 Bounds meshBounds = meshFilter.bounds;
 meshBounds.size = Vector3.Scale(meshBounds.size, mf.transform.localScale);
 meshBounds.center += mf.transform.localPosition;
 totalBound.Encapsulate(meshBounds);
avatar image mightychili Bunny83 · Nov 06, 2019 at 10:43 AM 0
Share

For me, subtracting the global position worked:

 meshBounds.center -= mf.transform.position;

However, I have didn't need to scale the size in my case, as I know that all of my sub-objects will be of (1,1,1) scale. Also, I have a bit of a different hierarchy, where I might have some empties in-between and stuff like that. And for a hierarchy like that, I feel like local position and scale make little sense, because a parent can be displaced and the child can be at a local position of (0,0,0). In general, using global positions and scales should work for both cases (OP's hierarchy and $$anonymous$$e), so I don't see a point of using locals. I haven't tested that though.

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

6 People are following this question.

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

Mesh collider not working as expected 2 Answers

Rotating/Aligning parents relative to their children 1 Answer

True scale of a visible GameObject with many childs 1 Answer

How Does Unitys PolygonCollider2D calculates its center ?? 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