Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by oliver-jones · Jun 09, 2020 at 07:34 AM · axisboundscenteroriginhandle

Reset GameObject Axis Origin at Model Center

I'm trying to find the centre point of a model, and reposition the axis (handle) to this point - actually, I'm spawning an Empty GameObject to this point, and then placing the model inside of this, and using the Empty GameObject as the parent (thus having the origin centred).

However my formula doesn't seem to find the centre correctly on most test models I have. I'm wondering if Unity has a built-in calc for this? I basically want to use the same results that the 'Toggle Tool Handle Position' does in the Editor;

alt text

Here is a model with a non-centred handle (looking top down);

alt text

Using the 'Toggle Tool Handle Position' to 'Center' places the handle perfectly in the centre of the model (as you'd expect). So my question is what is the calculation for this, so I can duplicate this via code?

NOTE; The purpose of this project is to allow the user import any FBX, so the centre point may have potential of being completely off, and out of designer control.



Currently, I cycle through all the children of the model, and use the mesh bounds to calculate a centre points. But it doesn't always give the correct results on certain models.

 private static Vector3 FindCentroid(Transform _parent, List<Transform> targets) {
 
         Vector3 centroid;
         Vector3 minPoint = targets[0].position;
         Vector3 maxPoint = targets[0].position;
 
         for (int i = 1; i < targets.Count; i++) {
 
             if (targets[i].GetComponent<MeshRenderer>() != null) {
 
                 Vector3 pos = targets[i].GetComponent<MeshRenderer>().bounds.center;
                 if (pos.x < minPoint.x)
                     minPoint.x = pos.x;
                 if (pos.x > maxPoint.x)
                     maxPoint.x = pos.x;
                 if (pos.y < minPoint.y)
                     minPoint.y = pos.y;
                 if (pos.y > maxPoint.y)
                     maxPoint.y = pos.y;
                 if (pos.z < minPoint.z)
                     minPoint.z = pos.z;
                 if (pos.z > maxPoint.z)
                     maxPoint.z = pos.z;
             }
         }
         centroid = minPoint + 0.5f * (maxPoint - minPoint);
         Vector3 _center = new Vector3(centroid.x, _parent.position.y, centroid.z);
         return _center;
     }


screenshot-2020-06-09-at-082639.png (10.9 kB)
screenshot-2020-06-08-at-173244.png (103.9 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 wewewu · Jun 09, 2020 at 09:00 AM

Probuilderize the Gameobject so you can change the pivot with probuilder.

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

128 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

Related Questions

LookAt - Change rotation origin point? 1 Answer

Degree from point? 2 Answers

Getting mesh length (x,z) 1 Answer

How would i find the center of an object with a Y axis offset to find the center on the top face? 1 Answer

Encapsulate.Bounds is no longer working. 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