Weird spike in Input.GetAxis when cursor set to locked in Start()

Hi, I’m following Brackeys’ tutorial on making an FPS controller. My current problem is when the game starts and I first moved my mouse, the value returned from Input.GetAxis for “Mouse X” and “Mouse Y” will have a sudden spike, causing the direction my player is looking at to change drastically. In the Start() function, I’ve set the

Cursor.lockState = CursorLockMode.Locked;

I guess it’s because it’s moving the cursor to the center of the screen, causing the spike. I looked around in the forum but didn’t see any solutions here. Is there any way to still keep the cursor locked but the spike won’t appear? Any help is appreciated, thanks!

Cursor.lockState don’t generate any input, only your real mouse movement will be written into GetAxis. Temporary remove cursor lock and check if bug still happenning.
I don’t remember Brackeys code exactly, but if you have something like that:

private float horizontalRotation;

private void Update()
{
    horizontalRotation += Input.GetAxis("Mouse X");
    bodyTransform.eulerAngles = new Vector3(0, horizontalRotation, 0);
}

Then you probably need to set player rotation into 0, 0, 0 in the Start method or in the inspector, save initial rotation or use

transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0), Space.Self);

Thanks for replying! After some digging, I think Input.GetAxis registered the delta between where my cursor was when I hit the start button and the center of the screen. If I didn’t lock the cursor then there won’t be the spike.

I solved it with a boolean to skip the first non-zero value that Input.GetAxis returned, so the spike won’t affect my view and rotation. Not too elegant but the only solution that works for me. Just leaving it here if anyone runs into the same problem.

3 Likes