Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Smithy311 · Mar 08, 2015 at 08:17 PM · c#rotationmousequaternionmouselook

Using quaternion for mouse movement?

Hi, I was wondering if it is possible to use quaternions for mouse movements, specifically the Y axis, rather than eulerangles? I was wondering if this is possible because eulerangles causes issues with some of the mechanics for my game.

Comment
Add comment · 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 maccabbe · Mar 08, 2015 at 08:23 PM 0
Share

$$anonymous$$ouse movement is a Vector2, do you mean camera rotation?

avatar image Smithy311 · Mar 08, 2015 at 09:06 PM 0
Share

Yeah, sorry, I meant camera rotation. I've got the X axis of the camera working fine, but the Y axis needs tweaking.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by S_Darkwell · Mar 09, 2015 at 01:03 AM

Try this:

  • "mouseInputX" should be your mouse's X movement.

  • "sensitivty" sets how much the movement of the mouse affects the rotation.

  • "Time.deltaTime" can be replaced with "Time.smoothDeltaTime" for a smoother effect.

         using UnityEngine;
         
         public class MouseRotate: MonoBehaviour
         {
             public float MouseInputX;
         
             private float sensitivity = 10.0f;
             
             void Update()
             {
                 transform.rotation = Quaternion.AngleAxis(MouseInputX * sensitivity * Time.deltaTime, Vector3.up);
             }
         }
    
    

Hope that helps!

Comment
Add comment · 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
0
Wiki

Answer by Waelwindows · May 11, 2015 at 01:23 PM

Yeah you should use a Quaternion for gameObject's rotation ( this case a camera ).

 using UnityEngine;
 
 public class CameraRot : Monobehaviour
 {
     void Update()
     {
         transform.rotation = Quaternion(X,Y,Z,W)
     }
 }

Of course you will edit the "X,Y,Z,W" letters for their Respective coordinates, or you could replace all of the "Quaternion ( )" thing with a variable, but of course that variable will need the Quaternion

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 Bunny83 · May 11, 2015 at 01:38 PM 0
Share

Uhm, that's pretty pointless. The 4 members of a quaternion aren't coordinates or angles. They represent a complex number system with 3 imaginary parts. You almost never want to set or modify the components of a quaternion directly.

avatar image
0

Answer by Bunny83 · May 11, 2015 at 01:59 PM

Well, you didn't specify your actual requirement how your input should work. Here i made a mouse look / orbit script which is purely based on quaternions. The rotations never suffer from any gimbal lock or strange flipping due to world axis bounding. However it really let's you rotate freely around the object which means you can end up in any orientation (great for space simulations).

If you want it to be bound to world axes you kind of have to use eulerangles in some way. Otherwise if you only use relative rotations you might slowly get "errors" in the sense of rotation around an axis you don't want.

For example if you replace this line:

 r = Quaternion.AngleAxis(Input.GetAxis("Mouse X"), transform.up) * r;

with

 r = Quaternion.AngleAxis(Input.GetAxis("Mouse X"), Vector3.up) * r;

You rotate around world up axis and around local x axis. So in theory you can't rotate around z that way. Though due to floating point precision problems after rotating a bit you might notice a slight tilt around z.

So if your rotations should be aligned with world axes you might want to use absolute angles.

 float polar = 0;
 float elevation = 0;

 // LateUpdate
 polar += Input.GetAxis("Mouse X");
 elevation -= Input.GetAxis("Mouse Y");
 r = Quaternion.AngleAxis(elevation, Vector3.right);
 r = Quaternion.AngleAxis(polar, Vector3.up) * r;

In this case the absolute rotation is created from distinct angles. Keep in mind if your elevation goes past +- 90° the y rotation seem to be inverted since you're actually upside down. You still rotate in the same direction but you see it inverted. (Like if you drive an RC car towards you then left / right seems to be inverted).

Comment
Add comment · 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

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

24 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

Related Questions

Keep objects current rotation, with Input Mouse X 0 Answers

How to Change rotation while preserving local horizontal rotation 1 Answer

Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles 0 Answers

Flip over an object (smooth transition) 3 Answers

How to make a plane face a static but rotating camera? 3 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