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.
Mouse movement is a Vector2, do you mean camera rotation?
Yeah, sorry, I meant camera rotation. I've got the X axis of the camera working fine, but the Y axis needs tweaking.
Answer by S_Darkwell
Mar 09, 2015 at 01:03 AM
"mouseInputX" should be your mouse's
"sensitivty" sets how much the
movement of the mouse affects the
"Time.deltaTime" can be replaced with
"Time.smoothDeltaTime" for a smoother
public class MouseRotate: MonoBehaviour
public float MouseInputX;
private float sensitivity = 10.0f;
transform.rotation = Quaternion.AngleAxis(MouseInputX * sensitivity * Time.deltaTime, Vector3.up);
Hope that helps!
Answer by Waelwindows
May 11, 2015 at 01:23 PM
Yeah you should use a Quaternion for gameObject's rotation ( this case a camera ).
public class CameraRot : Monobehaviour
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
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.
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;
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;
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).
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
26 People are following this question.
Keep objects current rotation, with Input Mouse X
Flip over an object (smooth transition)
Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles
Turret Continuing to Max Rotation - Following Mouse Cursor
OnDrawGizmos Debug.DrawLine position is pinching when rotated.