Mouse Look Script Not Working
I am making a script that will make the camera move when my mouse moves. It will also confine the cursor to the center of the screen unless the escape key is pressed. This code is not working, I would really appreciate any advice as to why my script is not working. In case you are wondering, the code checks out but the screen tilts. You can copy this script if you would like to see what I mean by "tilt".
Here is the code:
pragma strict
function Start () { Cursor.lockState = CursorLockMode.Locked; }
var horizontalSensitivity : float = 10; var verticalSensitivity : float = 10;
function Update () { var h = horizontalSensitivity Input.GetAxis ("Mouse X"); var v = verticalSensitivity Input.GetAxis ("Mouse Y"); transform.Rotate (-v,-h,0); outOfLock(); }
function outOfLock () { if (Input.GetKeyDown(KeyCode.Escape)) { Cursor.lockState = CursorLockMode.None; } }
Answer by Robert_s_a · Mar 12, 2017 at 02:03 AM
@crazygamerdl1 Hi,
The problem you're encountering is because you are rotating around two axes, and once you have rotated even a little bit, the axes are now tilted, so subsequent rotations will tilt the horizon. This is much more apparent if you have something to look at. If you're interested in a visual of what is happening, you can create three cylinders called Xaxis, Yaxis, and Zaxis, scale them all to (0.1, 2, 0.1), and then rotate them in the Inspector so that they are pointing along each axis. (No rotation on Yaxis, (0, 0, -90) on Xaxis, and (90, 0, 0) on Zaxis) then translate them all to be at the same location as your camera. From there, in the hierarchy, attach them as children to the camera. From here, you can run your script and watch your camera tilt and turn in the Scene window. You'll see how combinations of rotations in X and Y will result in the whole thing turning and twisting, because you are rotating around axes that are tilted.
If you're not interested, you can just proceed to the script below which will do what I think you are expecting. I did this using Quaternions and keeping a record of the total rotation you have input. Then I actually set the value of transform.rotation based on this quaternion. You can't plug numbers directly in this way without the quaternion .Euler transformation because quaternions aren't really numbers that humans use. LOL Anyway, basically I keep track of the total rotation you input, and send that in to your camera as if it were one rotation from (0,0,0) to wherever you have rotated to in total, rather than doing it in step by step increments which proceed from the last rotation. (I hope I'm not confusing you with this explanation as much as I'm confusing myself trying to type it) Essentially, you can spin left/right and up/down all you want, and the horizon will stay horizontal.
Enough babble - on to the code. I'm in C#, so you'll have to tweak the syntax to get it into Java:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCamera : MonoBehaviour {
private float horizontalSensitivity = 10.0f;
private float verticalSensitivity = 10.0f;
private float v;
private float h;
private float totalV;
private float totalH;
private Quaternion tempRotation;
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
// Read user input
h = horizontalSensitivity * Input.GetAxis("Mouse X");
v = verticalSensitivity * Input.GetAxis("Mouse Y");
// Accumulate total rotation from all input to date
totalV = totalV -= v;
totalH = totalH -= h;
// Calculate the single Quaternion rotation necessary to get us here
tempRotation = Quaternion.Euler(totalV, totalH, 0.0f);
// Apply that single rotation to the camera (from the origin)
transform.rotation = tempRotation;
outOfLock();
}
private void outOfLock()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
}
Hope this helps. :)
I tested your code and, it had the same problem I did. It would tilt although it was not as bad as it was in my code.
I did notice one error in my code above - doesn't affect things in C#, it's just redundant, but it may act differently in Java. The lines totalV = totalV -= v; (and the same for h) should just be totalV -= v; (or h). I can't see that causing problems with tilt though. On my machine, I can move the mouse all over, in any direction and the horizon stays horizontal without any tilt. I can tilt up, all the way around until I'm upside down, and continue until I'm right side up again, at any angle left or right, and the horizon stays flat. I'm not sure what else to suggest. Anyone else with input?
Is there anything else which affects the orientation of your camera? If so, that will need to be looked at as well, because that could be causing a tilt. That's the only other thing that I can think of at this point.
Your answer
Follow this Question
Related Questions
Rotation smoothing 0 Answers
Rotate towards mouse pointer 1 Answer
TopDown Look at mouse 0 Answers
How Can I Convert Vector3 From Local To Global(World)? 0 Answers
Object Rotate with Camera 2 Answers