Mouse movement with Input.GetAxis / Input.GetAxisRaw shows erratic behaviour when cursor is locked

Hi, I’m one of the Unity developers working on 3D Aim Trainer, a browser based aim trainer. It’s important to us that we can deliver a smooth experience for all of our users. Unfortunately, some of our users have reported erratic mouse movement, making it hard for them to effectively train their aiming skills. We’ve set up some tests and have managed to reproduce the issue ourselves.

Issue:
Input.GetAxis() and Input.GetAxisRaw() will frequently produce incorrect values for both “Mouse X” and “Mouse Y” in WebGL builds when running on lower end systems using mice with a high polling rate (500Hz and up). We have not been able to test whether this issue is unique to WebGL or if it occurs in standalone windows builds as well. The issue occurs only when the cursor is locked and causes the camera controller in our project to randomly rotate several degrees across a single frame, effectively making the camera skip.

As the setup required to reproduce this issue requires specific hardware and / or a virtual machine, I’ve also recorded a video showcasing the issue which you can find in the google drive link at the bottom of the post.

Steps to reproduce:

  • Visit the sample project page (itch.io) on a lower end system, using a mouse with a high polling rate. Target framerate should be right around 60 or a little below. In our tests we used an Asus Vivobook S15 using an Intel i3-8130U with built in graphics. A virtual machine with a similar setup may work too, though we haven’t tested this. As for the mouse, we used a Logitech G900 and a Corsair Ironclaw, both set to 1000Hz.
  • Press ‘P’ to lock the cursor and confirm it is locked.
  • Move the mouse left and right in long smooth motions
  • If the setup is correct, you should notice occasional skips in the camera movement. The absolute value of Input.GetAxisRaw(“Mouse X”) is also displayed on the Y axis in the graph along the bottom of the screen, and will show spikes coinciding with these camera skips.

Notes:

  • The problem seems to be directly linked to polling rates, as the frequency of these skips decreased alongside the polling rate. At 1000Hz the skips were most frequent, with a slight decrease in frequency at 500Hz, and with the issue being all but gone at 250Hz and 125Hz.
  • We noticed the same issue being present in other WebGL games; most notably in krunker.io, one of the leading WebGL-based first person shooters. This seems to indicate it’s not unique to our project but rather caused by an external factor.

Is this an issue specific to Unity / WebGL, or is there something wrong with the way we’ve implemented this? Can we do something to fix / mitigate this issue in our project? This is affecting a fair amount of our users and we would like to be able to offer them a smooth experience without having to compromise on their polling rate.

You can check out the source project along with a video showcasing the issue in our trainer here: google drive.

1 Like

I have this problem since a long time on my games (fps games).
I called this “the jump bug”, you look around and boom, your crosshair jump a bit without any reason.
I never reported it because it’s really hard to reproduce.

I just know that this problem have started for me when i have moved on windows 10, i’m not sure if it can be a factor.

@roka How did you manage to lock the mouse cursor on the crosshair in your webgl games?

This happens with my games as well and it’s really annoying. Players complain about it and I have no idea how to fix it

Bump
This actually affects a lot of my players and I regularly get complaints about it
It’s been happening for years and I have no idea how to fix it.

Note in the demo provided in the original post, it happens more if you turn the sensitivity down to the lowest

Hey @kiamvdd , not sure if you’re still around on the forums… I wanted to say that your test project is absolutely excellent. Any chance that the sources to this project might still be available? The Google Drive is unfortunately not alive anymore.

@nxtboyIII what version of unity are you using? I fixed many bugs in mouse movement + cursor lock since 2020, I’m curious where you’re seeing the problem?

I’m in Unity 2021.3.30f1

I get an issue where the mouse jumps if I start my look near the top or bottom of the canvas. Only on Windows browsers. Mind you, pressing the mouse button is required to look in our app.

Turns out, that it’s any edge. Left, right, top, bottom. Doesn’t matter. Unity 2023.3.11f1. Seems that after the javascript call to lock the pointer that the mouse delta spikes hugely in the next frame and causes the camera to jump horrendously

@iSeeGreg do you have a project you can share with me that shows this issue in 2023?

This issue is present since many years now but i’m done with it and with all the reports.

Other thread : https://discussions.unity.com/t/930168

Possible solution (do not have tested it yet) : https://discussions.unity.com/t/917666

Thanks @unityruba I have sent you a DM

@roka thanks for sharing the forum posts. I’m sorry it wasn’t addressed in them, it’s hard to keep track of everything all at once :frowning: I’ll set some time to look through the threads you shared.

@iSeeGreg yes, thank you! I’ll take a look today.