• 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
Question by SilverLifeee · Sep 29, 2021 at 12:20 PM · rotationquaternionrotate objecteuleranglesrotation axis

Why is this rotation not performed as expected?

Situation:
Assume you have a camera somewhere in worldspace and a GameObject "go1" on the same $$anonymous$$erarchy-level as the camera. go1 and the camera are rotated somehow in worldspace. Wit$$anonymous$$n go1 you have another nested gameobject go2.
Before any of the following approaches, you call in script:
go2 .transform.rotation = camera.transform.rotation;

After that, two different approaches lead to different results:

Approach1 (in editor):
Now you see the new local rotation wit$$anonymous$$n go2´s transform in the inspector. Now, if you change go2´s z-rotation value to 0, everyt$$anonymous$$ng happens as expected. The go is rotated around z-axis and basically go2´s local z-rotation is reverted

Approach2 (in code):
In contrast, if I calculate the Inspector´s local z-rotation "calcZRot" as described here, and call:
go2.transform.localRotation = Quaternion.AngleAxis(-calcZRot, go2.transform.forward)
Usually I expect the same t$$anonymous$$ngs will happen as in the Editor-Approach, but it doesn´t! The rotation is complete nonsense and not reverting the local z-Rotation of go2.


Basially, the Rotation of go2 should behave same as:

LookRotation(camera.transform.forward, go1.transform.up);
I Just like to know, why t$$anonymous$$s wrong rotation happens, even if there is a solution for t$$anonymous$$s. It seems like the rotation is simply not beeing performed around z-Axis.

Comment
Bunny83

People who like this

1 Show 2
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 bdubbert · Sep 29, 2021 at 03:34 PM 0
Share

When you say the rotation is complete nonsense, is the actual orientation of the object different between the two approaches? Or are the numbers showing up in the inspector just different?

avatar image SilverLifeee bdubbert · Sep 30, 2021 at 05:56 AM 0
Share

The Rotation is different between two approaches, simply because somehow it does not rotate the object around z-axis.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · Sep 30, 2021 at 08:45 AM

You have the wrong idea of quaternions ^^. A quaternion is always a relative rotation around a single axis. To represent an absolute orientation with a quaternion (w$$anonymous$$ch we do with transform.rotation) it has to represent a rotation that brings your coordinate system from the initial orientation to the target rotation. Imagine a simple case where the camera is first looking straight without any rotation. If you rotate the camera 90° to the right it means it's forward axis will change from (0,0,1) to (1,0,0) since the camera is now facing to the right. However w$$anonymous$$ch rotation axis gets you there? Exactly, a rotation around the y axis. That would be the axis you have to specify for AngleAxis.


Most prople make the mistake of still t$$anonymous$$nking in terms of euler angles. Euler angles are 3 seperate rotations around 3 axis performed one after the other in a particular order. A quaternion represents the combined rotation as one single rotation. The actual rotation axis of the localRotation could be any uninuitive axis since it essentially has to rotate your object wit$$anonymous$$n the local space of the parent from the not rotated state to the target rotation with a single rotation axis.


It's actually an amazing fact that you can rotate toany orientation in space by just a single axis rotation. It's difficult to tell whats your exact goal and why you need t$$anonymous$$s specific setup. Why don't you use LookRotation? Keep in mind that LookRotation also works for local rotations as long as the direction vectors you use are in that local space the rotation lives in. That means the parent coordinate space.


T$$anonymous$$s question somehow reminds me of t$$anonymous$$s question we just had about 2 weeks ago. A different case but it also has to do with manipulating the c$$anonymous$$ld rotation so it aligns with a worldspace orientation, though with an offset.

Comment
SilverLifeee

People who like this

1 Show 1 · 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 SilverLifeee · Sep 30, 2021 at 10:20 AM 0
Share

Thanks for the detailled response. I actually do use lookRotation now, but anyways I want to know why my described approach does not work. To answer your question about my goal: Imagin a sphere with its position at the world origin. (lets name it earth).
Now I have a coordinate system which is always perpendicular to the earth surface (lets call it perpendicularPlane). The camera is in another coordinate system, which hast the same orientation as the perpendicularPlane. Within the perpendicularPlane-coordinate-system , I have a lot of objects, which should always have the same orientation as the camera but with the constraint that every roll-angle of the cam (means z-rotation) needs to be reverted (that means, every object´s x-axis is perpendicular to the perpendicularPlane).
So what I was thinking , if I first set the object´s rotation equal to the one of the camera and afterwards rotate them back around their forward vector, I should be able to revert the unwanted rotation.
This actually works, but only with Approach1, where I set the z-rotation in inspector to zero, but with Approach2 this does not work. And I would really like to know, why this does not work because I think this knowledge is really important for further developement with unity.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

173 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

Related Questions

Set Rotation on Multiple Axis 1 Answer

Change rotation of an object based on an HTC Vive Controller 1 Answer

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

Combining two Quaternion rotations together 3 Answers

Rotate with raw gyro data. 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