Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
avatar image
0
Question by krispastas · Jan 05, 2021 at 03:29 PM · quaternionmouselook

Mouse look script with no world up axis behaves weirdly

So i have a mouse look script for a space game, where the world doesn't have an 'up' direction. It is quite hard to explain my problem, i will try to visualize it.alt text

As you can see, the view doesn't get back to normal but also rotates by a bit. What is the problem? The line that does everything:

transform.localRotation *= Quaternion.Euler(-GetMouseY(), GetMouseX(), 0);

The GetMouse() functions are just Input.GetAxis("Mouse X/Y")

why-tho.png (96.3 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
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jan 05, 2021 at 04:25 PM

If I understand correctly, you are asking why applying only X and Y rotations are resulting in a net rotation in the Z axis after a while.


When you rotate a rotated object it rotates it based off of its local rotation. Imagine in your mind that you rotate an object on just on the X axis by 40 degrees so that its now looking down a bit. Then rotate it from this position on its Y axis 10 degrees to its left. Then rotate it back 40 degrees in its X axis the other way. (Rot x by 40, rot y by 10, rot x by -40). This does NOT result in a net rotation of 10 degrees in the Y axis after all is said and done. That is because the rotations are being applied locally, but since the local rotation has changed due to the y rotation, the are not the same net rotation and therefore do not cancel each other out. On top of that, even though you only applied local X and Y rotations, you will notice that you get a small net rotation in the Z axis when all is said and done.


Hopefully that helped you understand whats happening, but if not don't worry because there is a simple fix. Instead of applying small delta rotations every frame to your rotated object, just keep track of a TOTAL rotation and apply it completely every frame. Something like this:

 private Vector3 localRot;
 
 void Update(){
     localRot += new Vector3(MouseY, MouseX, 0);
     transform.localRotation = Quaternion.Euler(localRot);
 }

Now instead of applying rotations on top of rotations, you just have 1 rotation that you are applying which ensures that no net Z rotation is introduced.

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 krispastas · Jan 05, 2021 at 04:49 PM 0
Share

Wow! That worked absolutely brilliantly! I modified a code a bit for my needs.

             if(localRot.x>270f || localRot.x < 90f)
             {
                 localRot += new Vector3(-Get$$anonymous$$ouseY(), Get$$anonymous$$ouseX(), 0);
             }
             else
             {
                 localRot += new Vector3(-Get$$anonymous$$ouseY(), -Get$$anonymous$$ouseX(), 0);
             }
             if(localRot.x>360f)
             {
                 localRot = new Vector3(localRot.x - 360f, localRot.y, localRot.z);
             }
             if (localRot.x < 0f)
             {
                 localRot = new Vector3(localRot.x + 360f, localRot.y, localRot.z);
             }
             if (localRot.y > 360f)
             {
                 localRot = new Vector3(localRot.x, localRot.y - 360f, localRot.z);
             }
             if (localRot.y < 0f)
             {
                 localRot = new Vector3(localRot.x, localRot.y + 360f, localRot.z);
             }
             transform.localRotation = Quaternion.Euler(localRot);

Gimbal lock is a bit of a problem right now, so I might use a better solution in the future.

Cheers!

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

114 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

Related Questions

Slerping screenshake 0 Answers

Slowly rotating an object while pointing at mouse 0 Answers

Using quaternion for mouse movement? 3 Answers

MouseLook script 90 degrees off 2 Answers

Using MouseLook script with LookAt function 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