- Home /
Rotating object with a mouse movement
Hi, I am trying to achieve mouse controlled rotation of a game object.
I am doing it very simply like this:
transform.Rotate(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
When the player moves the mouse up and down (Y axis on the screen) the object rotates up and down and the same goes for rotating left and right with X axis.
It works good, but the problem is, I only want the object to rotate up and down and to left and right. However with the solution above the object also rotates on its sides (as in twists). Imagine a shift stick in a car, you can rotate it up and down and to the sides, but you can't grab it and twist it like a screw.
I tried to fix the problem by doing this:
transform.Rotate(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,transform.localEulerAngles.y, 0);
The fix worked great, the object rotated up, down, left and right no problem (the twisting bug was gone), but it caused another problem instead, the object was no longer able to rotate lower than 90 degress (or 270 on the opposite side) for some reason.
I am completely out of ideas on how to fix this.
You know sometimes rotation doesn't work with the mouse. It's like when you try to rotate an object using the rotate tool but then it gets fucked up half way through. I think that's what's happening here.
@alucardj +1
ScreenToWorldPoint + LookRotation = and you can do it without eulerAngles and it's gimbal lock problem
Your answer
Follow this Question
Related Questions
Keep objects current rotation, with Input Mouse X 0 Answers
Flip over an object (smooth transition) 3 Answers
Need help on the 3ds max style camera control 0 Answers
Using Input.mouseposition to instantiate 1 Answer
Using quaternion for mouse movement? 3 Answers