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 /
  • Help Room /
avatar image
0
Question by tomomichalek · Nov 11, 2020 at 03:19 PM · positioningboundsencapsulation

Encapsulated bounds center error

Hi, I am trying to encapsulate multiple objets. I need to get capsule size as well as capsule rotation and position. My code is able to get correct size and rotation, but position is sometimes wrong. Here is my code:

 private IEnumerator EncapsulateObj()
     {
         Transform trackedTransform = trackedObj.transform;
         _renderers = trackedObj.GetComponentsInChildren<Renderer>();
         if (_renderers.Length == 0)
         {
             _capsule = new Bounds(trackedObj.transform.position, Vector3.zero);
             yield return null;
         }
 
         Quaternion originalRotation = trackedTransform.rotation;
         trackedTransform.rotation = Quaternion.Euler(0, 0, 0);
         bool foundNotNullRenderer = false;
         if (_renderers.Length > 0)
         {
             for (int i = 0; i < _renderers.Length; i++)
             {
                 if (_renderers[i])
                     if (!foundNotNullRenderer)
                     {
                         _capsule = _renderers[i].bounds;
                         foundNotNullRenderer = true;
                         continue;
                     }
                 _capsule.Encapsulate(_renderers[i].bounds);
             }
         }
         trackedTransform.rotation = originalRotation;
         positionHolder.transform.position = transform.TransformPoint(_capsule.center);
         positionHolder.transform.localScale = _capsule.size;
         positionHolder.transform.rotation = originalRotation;
     }

I guess its problem with pivot but I am not sure how to gix it.

I do place BoxCollider to represent bounding box. Yellow model is trackedTransform.

This is Good capsule alt text And this is wrong alt text

Thank you for any suggestions.

bad-box.png (230.2 kB)
good-box.png (245.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tomomichalek · Nov 12, 2020 at 08:26 AM

ok, so i guess i came up with the solution. Bassically generate 2 bounds, one will hold size, the otherone will hold position. To get size, original code is ok, to get center you won't rotate objects. It looks like this: _renderers = trackedObj.GetComponentsInChildren(); if (_renderers.Length == 0) { _capsule = new Bounds(trackedObj.transform.position, Vector3.zero); yield return null; } Transform holderTransform = positionHolder.transform; holderTransform.position = Vector3.zero;

         Quaternion originalRotation = trackedObj.rotation;
         bool foundNotNullRenderer = false;
         _capsule = new Bounds();
         Bounds centerBounds = new Bounds(); //to get center
         if (_renderers.Length > 0)
         {
             for (int i = 0; i < _renderers.Length; i++)
             {
                 if (_renderers[i])
                 {
                     Transform rendTrans = _renderers[i].transform;
                     Quaternion originalRot = rendTrans.rotation;
                     rendTrans.rotation = Quaternion.Euler(0,0,0);
                     if (!foundNotNullRenderer)
                     {
                         _capsule = _renderers[i].bounds;
                         foundNotNullRenderer = true;
                         rendTrans.rotation = originalRotation;
                         centerBounds = _renderers[i].bounds;
                         continue;
                     }
                     _capsule.Encapsulate(_renderers[i].bounds);
                     rendTrans.rotation = originalRotation;
                     centerBounds.Encapsulate(_renderers[i].bounds);
                 }
             }
         }

     holderTransform.position = transform.TransformPoint(centerBounds.center);
     holderTransform.localScale = _capsule.size;
     holderTransform.rotation = originalRotation;
     holderTransform.SetParent(Variables.Objects.initializeTransform.Value.parent);

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

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

Related Questions

Collider bounds encapsulate doesn't seem to do anything 2 Answers

I need to reposition the box collider of a cube and that cube is being generated at run time ? and pivot of the cube is also not correct. 0 Answers

How can i check if the point is within/inside certain area? 0 Answers

Syntax error when i try to simply modify transform position 0 Answers

Switch player model, positon, camera and animate 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