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 dude17943 · May 05, 2016 at 03:09 PM · gameobjectchildrenfindboundscenter

Center of a GameObject with Bounds?

I'm trying to find the center of a GameObject using the renderer bounds. Some of my GameObjects have a renderer on the parent where the script is attached, while others are grouped under an empty GameObject, so I need a method that accounts for both.

I am using the following code to try and add all the renderers of the children to a single object, then get the center point of the combined bounding box.

     protected Bounds _combinedBounds;
     protected Renderer _renderer;

     void Awake(){
         _renderer = GetComponent<Renderer>();

         if (_renderer){
             _combinedBounds = _renderer.bounds;
         }

         else{
             _combinedBounds = new Bounds();
         }
         
         Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>();

         if (renderers.Length > 0){
             for (int i = 0, len = renderers.Length; i < len; i++){
                 _combinedBounds.Encapsulate(renderers[i].bounds);
             }
         }

         Debug.DrawLine(_combinedBounds.center, new Vector3(0,50,0));    
 }


Below you can see that I've added the script to two separate GameObjects. You can also see that the results are way off center. Further, if I try to move the objects from those points, the bounds get even more off-center.

alt text

It seems like the farther I go from the center, the more off everything becomes. Does anyone know what I can do to get the center point of an object painlessly?

not-correct.png (33.9 kB)
Comment
Add comment · Show 9
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 Glurth · May 05, 2016 at 03:13 PM 0
Share

why is transform.position insufficient? IS it because you need to center of multiple objects? If so, would the AVERAGE transform.position work?

avatar image dude17943 Glurth · May 05, 2016 at 03:59 PM 0
Share

Transform.position doesn't always get the center of an object, especially groups of them.

See the image below. alt text

Aditionally, I'd still need to deal with the bounds in order to get things like the corners of the bounding box.

avatar image Glurth dude17943 · May 05, 2016 at 04:18 PM 0
Share

"Transform.position doesn't always get the center of an object"

This actually depend on how you define you model. If you create the mesh such that it's center is at model/local-coordinate (0,0,0), then transform.position, WILL always be the center of THAT mesh.

"groups of them": It's easy to compute the average position of a group of objects: If they are all the same size, this would in fact yield the center position of the set. If they are not all the same size, they wouldn't cut it.

"Bounding box" Well, THAT certainly eli$$anonymous$$ates using transform.position alone! $$anonymous$$y suggestion is now $$anonymous$$oot :)

avatar image Glurth · May 05, 2016 at 04:30 PM 0
Share

I guess it's not quite clear what "center" you mean. It looks to me, like you are computing the total bounds (and center) correctly (you can debug.drawline the bounds [xyz-$$anonymous$$-max] to confirm: the diagonals should intersect at the center). Perhaps the bounds-center is not what you want? Perhaps you want the center-of-mass, or center-of-volume?

avatar image dude17943 Glurth · May 05, 2016 at 05:01 PM 0
Share

alt text This is what I'm looking for. The GameObject on the left has one child inside it, and the one on the right has 4. I want to get the collective center of the entire group, as well as the bounds that confine them so that I can also check for the corners.

Here's another example:

alt text

Given a group of objects, I'd like to return their collective bounds. $$anonymous$$y understanding was that that was what Encapsulate allowed you to do, but it always returns a position completely off the rails from the actual object.

what-i-want.png (52.3 kB)
bounds.png (35.4 kB)
avatar image Glurth dude17943 · May 05, 2016 at 07:45 PM 0
Share

sorry dude, you obviously put effort into those pics, but I'm still unclear. If you can draw the current-result FINAL bounds value you get (after encapsulating child bounds), it might clear things up for me.
You second image is exactly what I would expect the results of your code to be; I must be misunderstanding the symptom.

Show more comments

1 Reply

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

Answer by Glurth · May 07, 2016 at 02:18 PM

AH HA! That last pic and notes had the key piece of information need to spot the bug: The fact that (0,0,0) was always included in the bounds.

The REASON this happens is: according to you code, if the root object does not have a renderer, you start the bounds off with:

 _combinedBounds = new Bounds();

This will create a new bounds object, with min, max, center of (0,0,0). When you call Encapsulate- you are extending the bounds to include the new boundaries, but this does NOT REMOVE the original bounds you started with, they are joined together. So, you newly encapsulated bounds will ALSO include (0,0,0).

To eliminate this error, do NOT initialize the bounds with new Bounds(), instead wait until you have an actual render object with bounds, and THEN start combining.

Comment
Add comment · Show 4 · 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 dude17943 · May 08, 2016 at 10:13 PM 0
Share

alt text

That was it.

 protected Bounds _combinedBounds;
      protected Renderer _renderer;
      void Awake(){
       
          Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>();
          if (renderers.Length > 0){
              _combinedBounds = (renderers[0].bounds);
              for (int i = 1, len = renderers.Length; i < len; i++){
                  _combinedBounds.Encapsulate(renderers[i].bounds);
              }
          }
          Debug.DrawLine(_combinedBounds.center, new Vector3(0,50,0));    
  }


file.png (14.9 kB)
avatar image Glurth dude17943 · May 12, 2016 at 09:09 PM 0
Share

looks good but watch-out: with this code it's possible for _combinedBounds to end up NULL (if no renders are found on startup), in which case this would generate a nullReferenceException : _combinedBound.center

avatar image tubswitch dude17943 · Jun 04, 2016 at 05:16 PM 0
Share

I tried using this, but when I used it on an animated object, the bounds never changed. How can I get the bounds on an object that is animated and have it update?

avatar image Glurth tubswitch · Jun 04, 2016 at 05:26 PM 0
Share

There are a few different bounds functions, some return world-coordinates, some return local-coordinates. $$anonymous$$ake sure you are not using the mesh bounds, for example, which returns local-coordinates.

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

63 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

Related Questions

How Can I assign a gameobject via script? 0 Answers

What to use instead of GameObject.Find and GetComponent 2 Answers

Get component from parent of children hittet with raycast 1 Answer

C# GameObject disabled scripts of its children after SetActive(false) 1 Answer

Custom inspector and GameObject Children 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