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 /
  • Help Room /
avatar image
0
Question by nitsnbolts_unity · Dec 10, 2020 at 01:11 PM · rotationquaternionaxiseulerangles

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B

I'm trying to set the rotation of a gameObject so that it's X,Y,Z rotation is based on 2 seperate gameObjects:

  • The Y and Z-Axis rotation is based on Y and Z-axis rotation of one gameObject.

  • And its X-Axis rotation is based on the Z-Axis localPosition of another gameObject.

Any idea on how to go about doing this?

What I have so far:

     Quaternion combinedQuaternion;
     GameObject mainGameObject;
     GameObject rotateAxisYZ;
     GameObject moveAxisZ;
     float xRotate, yRotate, zRotate;
     void RotateMainObject()
     {   
         // Setting x, y, z euler angle values of mainGameObject to rotateAxisYZ and moveAxisZ
         xRotate = moveAxisZ.transform.localPosition.z * 20;
         yRotate = rotateAxisYZ.transform.localEulerAngles.y;
         zRotate = rotateAxisYZ.transform.localEulerAngles.z;
         mainGameObject.transform.localEulerAngles = new Vector3(xRotate, yRotate, zRotate);

         // Combining euler angles into Quaternion form and setting it to mainGameObject rotation
         combinedQuaternion.eulerAngles = mainGameObject.transform.localEulerAngles;
         mainGameObject.transform.rotation = combinedQuaternion;
     }

This works well up to a point it seems. As long as the main gameObject is rotating on the X and Y axis alone - all's good. Once I introduce Z axis rotation - there's axis swapping and everything goes haywire. Anyone know of any possible solutions?

Comment
Add comment · Show 1
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 nitsnbolts_unity · Dec 10, 2020 at 12:40 PM 0
Share

Ok, I've just solved the problem. Was working on it for days and managed to solve it a few $$anonymous$$utes after posting haha. It was a rather simple fix. Here it is below:

  void Rotate$$anonymous$$ainObj()
     {
         Quaternion x = Quaternion.Euler(moveAxisZ.transform.localPosition.z * 100, 0, 0);
         Quaternion y = Quaternion.Euler(0, rotateAxisYZ.transform.localEulerAngles.y, 0);
         Quaternion z = Quaternion.Euler(0, 0, rotateAxisYZ.transform.localEulerAngles.z);
         Quaternion result = y * z * x;
         mainGameObject.transform.rotation = result;
     }

You can multiply the relevant floats with your input floats if need be. It was interesting to see that if the resulting quaternion was not multiplied in the order of y*z*x then the axes still do go nuts. I guess that's just the nature of matrix multiplications. Too bad it's not that intuitive. So a bit of trial and error is needed, but a very good and easy fix!

0 Replies

· Add your reply
  • Sort: 

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

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

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B 0 Answers

Using Local Angles to target enemy object 0 Answers

How to avoid single direction rotation with Quaternions? 0 Answers

Questions about Rotations in unity 1 Answer

Why isn't my steering wheel working? 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