I’ve been trying to get around this annoying bug for two days now and it’s driving me crazy. So I have a simple code that rotates a camera with the mouse, and moves forwards and backwards with the mouse wheel. Here’s my code:
void Start() {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update () {
RotationX = RotationX + Input.GetAxisRaw ("Mouse X") * MouseAcc * Time.deltaTime;
RotationY = RotationY + Input.GetAxisRaw ("Mouse Y") * MouseAcc * Time.deltaTime;
RotationY = Mathf.Clamp (RotationY, RotationMinMax.x, RotationMinMax.y);
transform.rotation = Quaternion.Euler (-RotationY, RotationX, 0);
if (Input.GetAxis("Mouse ScrollWheel") != 0) {
T = 0;
DirTo = transform.forward;
Dir = Input.GetAxis("Mouse ScrollWheel") / Mathf.Abs (Input.GetAxis("Mouse ScrollWheel"));
StopAllCoroutines ();
MoveCoroutine = StartCoroutine (MoveAbit());
}
chc.Move (Velocity * WalkSpeed * Time.deltaTime);
}
IEnumerator MoveAbit() {
while (T < 0.15f) {
Velocity = DirTo.normalized* Mathf.Clamp (Dir, -1, 1);
T+=Time.deltaTime;
yield return new WaitForSeconds (Time.deltaTime);
}
T = 0;
Velocity = Vector3.zero;
}
}
chc is my Character Controller btw.
In the editor it works exactly as it should. When I build it and run it google Chrome though, it behaves strange. After a lot of testing, I narrowed down the problem to the Cursor.LockState . If this is locked, the mouse scroll somehow affects the X and Y axis of the mouse. When I move the wheel, and after that move the mouse, the camera is rotated not slightly as it should by the X or Y axis, but instead it rotates as if the mouse scroll delta is added to the X and Y axis. It’s very strange and erratic behaviour.
When the Cursor is not locked, this problem doesn’t occur, but I need it locked because if it’s not, when the mouse goes out of the window you cannot move anymore.
And btw, I’ve tried to remove the part of the code that moves the camera with the scroll, and it still does the same. So event if I don’t use the scroll in my script, the camera is still affected by the scroll.
Anyway it’s a weird bug, I really hope someone has a workaround or a fix. Thank you!