Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 theLucre · Jun 10, 2014 at 05:18 AM · 2dscreenboundsboxcollider2d

How does Bounds.Encapsulate() work?

I have four bounding BoxCollider2D's used for level boundaries. I'd like to know the bounds of the entire level using these four colliders. I've tried the code below using Bounds.Encapsulate to widen the bounds for each BoxCollider2D but the debug results don't seem accurate. What is missing here?

 ScreenBounds = new Bounds(Vector3.zero, Vector3.zero)
 BoxCollider2D[] boxColliders = gameObject.GetComponents<BoxCollider2D>();
 foreach (BoxCollider2D boxCollider in boxColliders) {
     Bounds current = new Bounds( boxCollider.center, boxCollider.size );
     Debug.Log( "new bounds : " + current );
     ScreenBounds.Encapsulate( current );
     Debug.Log( "Encapsulate results : " + ScreenBounds.extents);
 }

Debug log values:

new bounds : Center: (-1.0, -13.6, 0.0), Extents: (22.5, 2.0, 0.0)

Encapsulate results : (22.5, 7.8, 0.0)

new bounds : Center: (-1.0, 13.6, 0.0), Extents: (22.5, 2.0, 0.0)

Encapsulate results : (22.5, 15.6, 0.0)

new bounds : Center: (-21.5, 0.2, 0.0), Extents: (2.0, 15.5, 0.0)

Encapsulate results : (22.5, 15.6, 0.0)

new bounds : Center: (19.3, 0.2, 0.0), Extents: (2.0, 15.5, 0.0)

Encapsulate results : (22.5, 15.6, 0.0)

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

2 Replies

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

Answer by Bunny83 · Jun 10, 2014 at 05:56 AM

You have two problems here:

  • you initialize your ScreenBounds with "new Bounds(Vector3.zero, Vector3.zero)". These "bounds" will be extended to include the given bounds. Since 0,0,0 isn't part of any bounds the result will be wrong.

  • you use the boxcolliders local center and local size and treat them like world space coordinates. So the result will never be right.

You have to initialize your ScreenBounds with the first collider in your array. This can be done like this:

 BoxCollider2D[] boxColliders = gameObject.GetComponents<BoxCollider2D>();
 ScreenBounds = new Bounds(Vector3.zero, Vector3.zero)
 foreach (BoxCollider2D boxCollider in boxColliders)
 {
     if (ScreenBounds.extents == Vector3.zero)
         ScreenBounds = boxCollider.bounds;
     ScreenBounds.Encapsulate( boxCollider.bounds );
 }


An alternative would be:

 BoxCollider2D[] boxColliders = gameObject.GetComponents<BoxCollider2D>();
 if (boxColliders.Length > 0)
 {
     ScreenBounds = boxColliders[0].bounds;
     foreach (BoxCollider2D boxCollider in boxColliders)
     {
         ScreenBounds.Encapsulate( boxCollider.bounds );
     }
 }
 else
     ScreenBounds = new Bounds(Vector3.zero, Vector3.zero)

ps: You should always use the .bounds property since boxCollider.center and boxCollider.size are in local space. So center is just the offset from the gameobjects pivot. The bounds property also considers if the collider / gameobject is rotated / scaled.

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 theLucre · Jun 21, 2014 at 01:56 AM 0
Share

awesome! thank you. i am using BoxCollider2D and there doesn't appear to be a bounds property, even though it's in the docs... not sure why that is :/

"Type UnityEngine.BoxCollider2D' does not contain a definition for bounds'"

update: it must have been my version on mac, just updated and bounds displayed properly :)

avatar image
3

Answer by ayyappa · Dec 29, 2014 at 07:54 PM

For those who has some more confusions, here is the code which can explain in understanding. Basically we are considering first one as initial bounds and make union with other bounds. If we include empty bounds initially it will try to enclose that point as well which is not we expect.

 Bounds bounds;

 void CalculateBounds()
         {
 
             Bounds tempBounds =  new Bounds();
 
             //Calculate the bounds of enclosed renderers
             for(int i=0; i<m_listOfRenderers.Count; i++)
             {
                 Renderer each = m_listOfRenderers[i];
 
                 if(i == 0)//Copying first one initially
                 {
                     tempBounds = each.bounds;
                 }
                 else
                 {
                     tempBounds.Encapsulate(each.bounds);
                 }
             }
             
             //Assign it to original bounds.
             bounds = tempBounds;
         }
 
 
 
 void OnDrawGizmos() 
         {
             Gizmos.color = Color.yellow;
             Gizmos.DrawWireCube(bounds.center, bounds.size);
 
             Gizmos.color = Color.red;
             Gizmos.DrawSphere(bounds.center, 0.1f);
         }

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

23 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

Related Questions

Encapsulate Bounds Problem [SOLVED] 1 Answer

BoxCollider2D not colliding as expected 0 Answers

How to set screen boundaries? 1 Answer

My RigidBody2D+BoxCollider2D is getting stuck on corners 1 Answer

touch position to world position 2D 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