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 /
avatar image
0
Question by FoxAlive555 · Oct 06, 2017 at 12:12 PM · rotationquaternioneuler angles

Rotating an object around the centre of its mesh, using Quarternion AxisAngle

I'm working on a legacy project made entirely by someone else.

A group of objects called "surfaces" are instantiated using data from a JSON file. The meshes assigned to these objects are generated procedurally, so from what I can tell, the rotation origin is offset by a different amount for every object.

If I use RotateAround, with the point being the center of the mesh, the Euler rotation produces completely the wrong set of rotations, but in the right position.

Using Quaternion.AxisAngle produces the correct rotations, but in the wrong position.

Using the rotation tool in the editor at runtime produces perfect results, as the tool seems to know to take the center of the mesh as the origin for rotation, and simply translates the object to compensate.

Is there a way to replicate what the Editor rotation tool is doing in a script?

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
1
Best Answer

Answer by BlakeSchreurs · Oct 06, 2017 at 12:46 PM

So, I see a few ways that I might try to approach this problem. The best way to do it is going to depend a lot on your application and the geometry you're creating.

1) Cheat: Child your generated object to an Empty GameObject (Make sure the generated GO is at 0,0,0 relative to the parent), and move the parent around. Sometimes this simplifies things, sometimes it doesn't, but it's worth a quick try.

2) Calculate the "Center of mass" of the generated object. Here's the gist: After it's generated, get the mesh from the MeshFilter. Sum up all of the vectors in the mesh, divide by the number of vectors, to get an average position. (Note: If one side has a lot more triangles than another, the center will trend toward the side with more triangles). You can then try an offset by this position, or go through and subtract this position from all the vectors to re-center it a bit.

3) Calculate the "render center" of the generated object. Here's the gist: After it's generated, get the mesh from the MeshFilter. Find the min and max x,y,z values for all vectors in the mesh. Take the max, subtract the min, then divide by two (Note, if the model has tall bits like a space station with antennas, this center will tend more toward those tall bits). You can then try an offset by this position, or go through and subtract this position from all the vectors to re-center it a bit.

Good luck!

Comment
Add comment · Show 2 · 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 FoxAlive555 · Oct 06, 2017 at 03:12 PM 0
Share

Thank you for this reply,

I ended up completely altering how objects were placed after they were instantiated to prevent getting that original offset.

$$anonymous$$y exact problem was pretty specific so unfortunately that "cheat" method didn't work (it was the first thing I tried).

I strongly, strongly suspect that all your answers would be applicable to many people who had more general rotation problems in Unity, and you also taught me some new techniques. I will definitely be using those in future. Accepting this because I think it will be more useful to others looking up this question than the solution I used.

avatar image Glurth · Oct 06, 2017 at 03:38 PM 0
Share

Re #3) I should mention the BOUNDS class. both $$anonymous$$eshs, and $$anonymous$$eshRenderes provide bounds members. (one in model-space, one in work-space, repecively). This class $$anonymous$$AY contain the "center" point you are looking for: https://docs.unity3d.com/ScriptReference/Bounds.html

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

101 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

Related Questions

How to convert quaternion to vector3 with specific multiplication order? 5 Answers

Duplicating rotation with axis constraints 1 Answer

Rotating player with Quaternion and RotateTowards 1 Answer

Get player orientation, but only one Euler angle is non-zero 2 Answers

Smoothly rotating a gameObject 90 degrees on a button press and being able to rotate it in the opposite direction off of the same button. 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