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
3
Question by sfilo · Jun 29, 2016 at 05:35 PM · rotationtransformquaternionaxis

Problem using Quaternion.AngleAxis around transform.up

Hello boys and girls

I am missing something fundamental about Quaternions and can't find the answer on the forums.

Here is my setup: alt text
I have two identical game objects
The bottom one is rotated so that local up points to the left, the top one is not rotated
Both objects have the same component with one line of code on Start

 void Start () 
     {
         transform.rotation = Quaternion.AngleAxis(45, transform.up);
     }

The top object rotates around it's up axis as expected.
The bottom object rotates so that it's up axis changes. I don't understand why. I expect it to rotate around the local UP axis. I know that transform.up is the local up axis in world coordinates so I think it should work.

What am I missing? Please help!

screen-shot-2016-06-28-at-224953.png (90.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
3
Best Answer

Answer by Bunny83 · Jun 29, 2016 at 06:29 PM

You got something fundamental wrong here. Quaternions actually don't specify an "absolute" rotation but a relative. Unity simply defines the default orientation as z=forward y=up and x=right.

When you use Quaternion.AngleAxis you create a "relative rotation" which can rotate a vector or a coordinate system from an initial state by that relative amount. However you don't do a relative rotation but you set the "global" rotation of the object to that rotation. So you basically rotate the object from it's initial (0, 0, 0) state by the relative amount which is not what you expect.

To rotate the current orientation by that relative amount you have to multiply the current rotation by that relative amount:

 transform.rotation = Quaternion.AngleAxis(45, transform.up) * transform.rotation;

Note: Quaternion multiplication is not commutative, so the order of it's operands matters.

However it's usually easier to use the Rotate method :

 transform.Rotate(0f, 45f, 0f);

or

 transform.Rotate(Vector3.up, 45f);

or

 transform.Rotate(transform.up, 45f, Space.World)

All those 3 variants do the same thing as the AngleAxis variant i've posted.

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 Eno-Khaon · Jun 29, 2016 at 07:03 PM 3
Share

Following up on the noncommutative nature of Quaternion multiplication, just as a relative rotation can be multiplied by another:

 transform.rotation = Quaternion.AngleAxis(45, transform.up) * transform.rotation;

the same result can also be found by reversing the equation and replacing the relative axis with an absolute one:

 transform.rotation = transform.rotation * Quaternion.AngleAxis(45, Vector3.up);

(Of course, this essentially just matches transform.Rotate())

avatar image sfilo · Jun 30, 2016 at 10:01 AM 1
Share

Thank you Bunny83 and $$anonymous$$haon. Both explanation and examples were very helpful. I needed to rotate a camera on update around it's local up axis but the rotation had to be relative to the initial rotation, not cumulative. I ended up saving the initial rotation and multiplying it with a relative rotation around Vector3.up.

 Quaternion initialCameraRotation = camera.rotation;
 void Update()
 {
 transform.rotation = initialCameraRotation * Quaternion.AngleAxis(desiredRotation, Vector3.up );
 }

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

58 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

Related Questions

How to rotate on one axis while keeping the other axes open to be rotated by other scripts? 1 Answer

How can I multiply the rotation of a mirrored object? 1 Answer

How to instantiate on custom rotation? 1 Answer

Unity Rotate Raycast on Quaternion 1 Answer

Ball Rolling Animation Not Working Properly 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