Search Unity

Bug Mouse Delta is framerate-dependent

Discussion in 'Input System' started by Atomike, Jan 12, 2021.

  1. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    So I was trying out the new Input system and I noticed that for some reason the mouse delta in the new input system is framerate dependent. This is a major issue, especially since the Unity Editor's play mode has an uncapped framerate unless forced into a specific framerate via a script. This can lead to the input feeling completely different in the editor than it does in would in a build.



    Here's my code so you can see this isn't a coding error on my part. Everything is called by FixedUpdate and is interpolated over Time.fixedDeltaTime.
    Code (CSharp):
    1. public void HeadRotation()
    2.     {
    3.         Vector2 mouseInput = inputActions.Gameplay.Camera.ReadValue<Vector2>();
    4.  
    5.         lerpedMouseY = Mathf.Lerp(lerpedMouseY, -mouseInput.y * cameraSensitivity, Time.fixedDeltaTime * 15f);
    6.  
    7.         float rotValue = Mathf.Clamp(headRotation.localRotation.x, -0.6f, 0.6f);
    8.         Quaternion newRot = new Quaternion(rotValue, headRotation.localRotation.y, headRotation.localRotation.z, headRotation.localRotation.w);
    9.  
    10.         headRotation.localRotation = newRot;
    11.         headRotation.Rotate(new Vector3(lerpedMouseY, 0, 0));
    12.     }
    13.  
    14. public void BodyRotation()
    15.     {
    16.         Vector2 mouseInput = inputActions.Gameplay.Camera.ReadValue<Vector2>();
    17.  
    18.         lerpedMouseX = Mathf.Lerp(lerpedMouseX, mouseInput.x * cameraSensitivity, Time.fixedDeltaTime * 15f);
    19.  
    20.         transform.Rotate(0, lerpedMouseX, 0);
    21.  
    22.         skin.rotation = Quaternion.Slerp(skin.rotation, transform.rotation, Time.fixedDeltaTime * 5f);
    23.        
    24.  
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         BodyRotation();
    30.         HeadRotation();
    31.     }
     
  2. nongbenzgames

    nongbenzgames

    Joined:
    Oct 8, 2018
    Posts:
    19
    From what I understand, mouse inputs are updated each frame (Update). If you're just using FixedUpdate, you're missing out on many deltas with high framerates. They aren't queued up waiting to be read.

    Hope putting it in Update() (and tweaking sensitivity) will resolve the framerate dependence. But this new system is quite wonky at times so I could be wrong.
     
  3. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    I'll definitely give it a try. It's funny, usually I use FixedUpdate to avoid framerate dependency, now I'm having to try the exact opposite.
     
  4. nongbenzgames

    nongbenzgames

    Joined:
    Oct 8, 2018
    Posts:
    19
    Yes in this case it's because mouse inputs are just deltas (distance moved in a frame, not velocity) so it's already framerate independent. You need Time.deltaTime in your lerping but then it should work.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You can set the new input system to update during FixedUpdate rather than during Update.

    Reading input in the old input system was always wrong and would always lead to both losing input and delayed inputs. Putting stuff into FixedUpdate does not make it framerate independent at all. It still runs during an update, just not every update.