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 /
  • Help Room /
avatar image
1
Question by Izelude · Aug 24, 2016 at 01:16 PM · colliderboundsencapsulation

Collider bounds encapsulate doesn't seem to do anything

Hi, today i ran into the following problem: I need to add a (Box)Collider to a GameObject loaded from an AssetBundle. Since it is a complex geometry, i don't want to add colliders to all children of the root node, but rather just add a collider to the root node and grow it to the size of the full geometry.

From what i read so far all i would have to do is call collider.bounds.encapsulate on all the children of the root node. However when i do that, nothing at all changes about the collider, no matter what i try.

In the code below i made a simplified example of what i mean (instead of loading a geometry i just create 2 primitives) and in the comments you can see the Debug.Log result.

As you can see no matter what i try the bounds of the default boxCollider of my cube primitive stay the same.

So my question is what am i doing wrong??

 public class test : MonoBehaviour
 {
 
     void Start()
     {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = new Vector3(0, 0, 0);
 
         var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         Destroy(sphere.GetComponent<SphereCollider>());
         //I intentionally remove the collider, since i want to grow 
         //the cubes collider to include the sphere
         sphere.transform.SetParent(cube.transform);
         sphere.transform.localPosition = new Vector3(2, 0, 0);
 
         cube.GetComponent<BoxCollider>()
             .bounds
             .Encapsulate(sphere.GetComponent<Renderer>().bounds);
 
         Debug.Log(cube.GetComponent<BoxCollider>().bounds); 
         //Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
 
         cube.GetComponent<BoxCollider>()
             .bounds
             .Encapsulate(new Bounds(Vector3.zero, new Vector3(100, 100, 100)));
 
         Debug.Log(cube.GetComponent<BoxCollider>().bounds); 
         //Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
 
         cube.GetComponent<BoxCollider>().bounds.Expand(500);
 
         Debug.Log(cube.GetComponent<BoxCollider>().bounds); 
         //Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
     }
 }
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 magewe · Aug 26, 2016 at 11:13 AM 0
Share

I have a similar problem with bounds.Set$$anonymous$$in$$anonymous$$ax a.GetComponent().bounds.Set$$anonymous$$in$$anonymous$$ax(new Vector3(-1, -1, -1), new Vector3(1, 1, 1)); Debug.Log(a.GetComponent().bounds);

There isn't any change in the value of bounds... any help?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hectormateos · Jul 07, 2017 at 08:59 AM

Same problem here :S

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
avatar image
0

Answer by Yaeker · Jun 27, 2019 at 03:28 PM

Hi,

I know this is an old thread, but for new people coming here the solution follows the same principle as transform.position or most structs:

     BoxCollider cubeCollider = cube.GetComponent<BoxCollider>();
     Bounds b = cubeCollider.bounds;
     b.Encapsulate(sphere.GetComponent<Renderer>().bounds);
     cubeCollider.center = b.center;
     cubeCollider.size = b.size;
Comment
Add comment · 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 ahstein · Aug 25, 2020 at 08:41 PM 0
Share

I'm not sure if I should start a new thread or revive this old one to ask this...

How do you accomplish this for a Collider2D? Collider2D.bounds is read only and it doesn't have any public accessors for center/extents/etc. that I can find. Is it possible to change the size of a Collider2D at runtime?

EDIT: Never$$anonymous$$d, I figured it out. See here: http://answers.unity.com/answers/1765662/view.html

Leaving this comment in case someone finds it useful in the future.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the math behind Collider.ClosestPointOnBounds? 1 Answer

Problems with ClosestPointOnBounds 0 Answers

How to get raw bounds size of a Box collider? 1 Answer

ClosestPointOnBounds from within collider 0 Answers

Encapsulated bounds center error 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