Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Sisym · Jul 16, 2015 at 05:10 PM · quaternion

Quaternion.Euler tilts rotation on z axis, even though the z rotation is set to 0

So I've got a first person camera that rotates based on mouse movement, and I am assigning the camera's transform using a quaternion. However, when I create the rotation needed, the camera slowly tilts on the z axis, even though I have clearly set the z rotation to 0. I don't really know how to use Quaternions, and previous answers about this problem have been very specific to the question, and when I try to fix it, I get the exact same problem, no matter how many different ways I write the code differently.

Here is the code I am using:

 void Update ()
     {
         transform.rotation *= Quaternion.Euler (-Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X"), 0);
     }
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 hexagonius · Jul 16, 2015 at 05:55 PM 0
Share

First a guess. You want to yaw in world space (camera should rotate around the world up), while pitching should happen in local space.

Separate your axis and apply the yaw to the rotation of the transform and the pitch to the localRotation of the transform.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by maccabbe · Jul 16, 2015 at 05:57 PM

"Rotating by the product lhs * rhs is the same as applying the two rotations in sequence, lhs first and then rhs."

So let's try rotating the camera by applying a rotation (90, 90, 0) then (-90, 0, 0). Rotate the camera 90 degrees on the x axis then 90 degrees on the y axis and back 90 degress on the x axis. At this point you have a rotation which is a 90 degree turn on the z axis.

To keep the z rotation 0 you probably want to only apply one rotation on the x and one rotation on the y axis. For instance

 float xRot=0;
 float yRot=0;
 
 void Update(){
     xRot+=-Input.GetAxis("Mouse Y");
     yRot+=Input.GetAxis("Mouse X");
     transform.rotation=Quaternion.Euler(xRot, yRot, 0);
 }
Comment
Add comment · 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 Sisym · Jul 16, 2015 at 06:07 PM 0
Share

Thanks, I read somewhere that multiplying quaternions was the same as adding, I had no idea that was the problem.

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

22 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

Related Questions

Quaternion values at instantiation. 1 Answer

Rotate on 2 different axes 1 Answer

How to smoothly align an object to a surface, but only partially 2 Answers

Rotation 90.0001 and 1.001719 bugs! 1 Answer

Rotate an object relative to Camera 2 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