Input.GetAxis("Mouse ScrollWheel") is frame-rate dependent?

I did some research but still could not figure out why my code is still framerate-dependent.
It said in the document: “This is frame-rate independent; you do not need to be concerned about varying frame-rates when using this value.”
This code is a smooth zoom with scroll wheel, zoom in & out the camera’s pivot point.
The scroll amount is really small when the fps is low, and large when the fps is high.
How can I fix the problem? :confused:

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    public Transform pivot, mainCam;

    public float cam_distance, max_distance_from_pivot;

    public float zoom_speed, zoom_dampening;

    void Update(){
        Zoom();
    }
    void Zoom(){
        if (mainCam.localPosition.z != cam_distance * -1f){
            mainCam.localPosition = new Vector3(0f, 0f, Mathf.Lerp(mainCam.localPosition.z, cam_distance * -1f, Time.deltaTime * zoom_dampening));
        }

        if (Input.GetAxis("Mouse ScrollWheel") != 0f)
        {
            float ScrollAmount = Input.GetAxis("Mouse ScrollWheel");
            cam_distance *= 1 - (Mathf.Sign(ScrollAmount) * zoom_speed * 0.01f);
            cam_distance = Mathf.Clamp(cam_distance, .1f, max_distance_from_pivot);
        }
    }

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
public Transform pivot, mainCam;

public float cam_distance, max_distance_from_pivot;

public float zoom_speed, zoom_dampening;

private float targetCamDistance;

void Start()
{
    targetCamDistance = cam_distance * -1f;
    mainCam.localPosition = new Vector3(0f, 0f, targetCamDistance);
}

void Update()
{
    Zoom();
}

void Zoom()
{
    if (mainCam.localPosition.z != targetCamDistance)
    {
        float newZ = Mathf.Lerp(mainCam.localPosition.z, targetCamDistance, Time.deltaTime * zoom_dampening);
        mainCam.localPosition = new Vector3(0f, 0f, newZ);
    }

    float scrollAmount = Input.GetAxis("Mouse ScrollWheel");
    if (Mathf.Abs(scrollAmount) > 0.01f)
    {
        targetCamDistance *= 1 - (Mathf.Sign(scrollAmount) * zoom_speed * 0.01f);
        targetCamDistance = Mathf.Clamp(targetCamDistance, .1f, max_distance_from_pivot);
    }
}

}
Changes made:

  1. Moved the camera zoom amount calculation (targetCamDistance) out of the if condition to ensure it’s updated every frame, regardless of the current zoom state.
  2. Removed the condition on the Input.GetAxis("Mouse ScrollWheel") to always update the camera zoom amount, ensuring consistent zoom responsiveness.

If you use the Update method, you should not multiply Time.deltaTime within the Mathf.Lerp function. Doing so can introduce a frame-rate dependency to the interpolation, leading to inconsistent behaviour at different frame rates. Instead, apply Time.deltaTime outside of the interpolation factor to achieve a frame-rate independent effect.

As per the documentation, the return value of Input.GetAxis is frame-rate independent, however you’re clamping the delta value to its sign. Mathf.Sign will only return a value of +1 or -1 thus omitting delta time. If clamping the delta value to 1 is intended, use Mathf.Clamp(ScrollAmount,-1f, 1f) instead.

Also, due to floating point precision errors, never compare floats with the equality operator directly. Use Mathf.Approximately(ScrollAmount, 0f) instead.

float ScrollAmount = Input.GetAxis("Mouse ScrollWheel");

if (!Mathf.Approximately(ScrollAmount, 0f))
{    
    cam_distance *= 1f - (Mathf.Clamp(ScrollAmount,-1f, 1f) * zoom_speed * 0.01f);
    cam_distance = Mathf.Clamp(cam_distance, .1f, max_distance_from_pivot);
}