• 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 minutes 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Using Local Angles to target enemy object 0 Answers

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

Draw a ray from a gamobject and keep direction of the ray relative to the gameobjects rotation. 1 Answer

How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer

How to add to a rotation angle? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges