- Home /
Mouse input erratic
Hello,
I am trying to follow a tutorial to make a FPS controller. The first section is making the player turn left and right. The issue is that when I use the following code the camera turns erratically. It appears to be that the values are occationally massive rather than the correct delta.
void FixedUpdate()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
float forward = Input.GetAxis("Vertical") * speed * Time.deltaTime;
playerOBJ.transform.Rotate(Vector3.up * mouseX);
playerOBJ.transform.Translate(Vector3.forward * forward);
Debug.Log(forward * Time.deltaTime);
}
The code also tests the vertical axis and that has no problems so this isn't a frame rate issue just wierd value issue.
I have multiple monitors and using version 2019.4.2f1
Thanks!
Answer by Eno-Khaon · Jul 04, 2020 at 06:09 PM
Actually, this IS a framerate issue.
By multiplying a per-frame value by Time.deltaTime, you're causing it to become framerate-dependent. You'll want to crank your mouseSensitivity waaaay down (~1/60 for example, assuming your framerate has been capped).
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;// * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;// * Time.deltaTime;
Because the Mouse Axis inputs are already absolute changes to mouse position, they don't need to be given variable scaling.
I have tried both, with and without, its not high sensitivity but random values.
Enabling vsync fixes the issues. I think its a combination of windows multi screen stutter and high frame rate. In my test scene I am hitting 2000+ frames/s at least that is what stats is reporting. When I enable vsync I do not have the issue anymore.
Further to this I have the windows multiscreen stutter. enabling vsync and unplugging my other monitors does not produce this error anymore. What is odd however is how its getting its values, your talking every odd frame your getting an input value 10 or 100 times the size of normal values from the mouse input.
Ah, there's something I overlooked. If your framerate is that high, you're probably also losing a huge amount of input to reading mouse input from the wrong function. You want your input to be read from Update() ins$$anonymous$$d of FixedUpdate(), since that's where it's actually processed in the first place. With a framerate of 2000 fps, you're probably dropping 39 out of every 40 mouse inputs!
Furthermore, spikes in movement would then likely be caused every time something hits your framerate briefly. For example, if your framerate is 2000, then it drops to 100 (for one or two frames, for example), then your mouse input would suddenly spike from 1/40 frames registering input to 1/2, or a ~20x spike in movement registered for that frame.
Input is processed during, and should be read from Update(). Physics engine processing (like, Rigidbody.AddForce()) take place during FixedUpdate().
Thanks,
I have fixed it but also moved to the new input system so the implementation is slightly different now.
I think you are right but obviously haven't tested it. I also think the issue is with a know bug with windows 10 which causes stuttering on windows when using multiple screens (something they are apparently fixing come patch 2004!)
Your answer
Follow this Question
Related Questions
Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers
Cursor/Mouse movement to controller 0 Answers
Multiple Constant Force Inputs at once? 1 Answer
Input Axis Mouse ScrollWheel 1 Answer
OnMouse menu 1 Answer