Manually smooth Input.GetAxisRaw

I’m setting up the movement for my first person game, and while I found the Input.GetAxis to be too smooth and not responsive enough for what I’m trying to achieve, Input.GetAxisRaw feels too snappy. I would like to fine-tune the input smoothing and have tried with Mathf.MoveTowards but it didn’t seem to work, but maybe I didn’t use it correctly.

Extend it however you like.

    public float acceleration = 1f;
    public float deceleration = 2f;
    public float v = 0;
    private void Update()
    {
        float accelerating = Input.GetAxisRaw("YourAxis");
        if (accelerating != 0 || v > 0)
        {
            if (accelerating > 0)
            {
                v = Mathf.Clamp01(v + acceleration * Time.deltaTime);
            }
            else
            {
                v = Mathf.Clamp01(v - deceleration * Time.deltaTime);
            }
        }
    }

Just found out that you can control the smoothness for Input.GetAxis by changing the “gravity” and “sensitivity” in the Input Manager.

GetAxis uses Time.deltaTime to smooth the value by Sensitivity and Gravity settings from the Input configuration, therefore it respects Time.timeScale. You can use Time.unscaledDeltaTime like this if you need to ignore it.

private void GetSmoothRawAxis(string name, ref float axis, float sensitivity, float gravity)
{
    var r = Input.GetAxisRaw(name);
    var s = sensitivity;
    var g = gravity;
    var t = Time.unscaledDeltaTime;

    if (r != 0)
    {
        axis = Mathf.Clamp(axis + r * s * t, -1f, 1f);
    }
    else
    {
        axis = Mathf.Clamp01(Mathf.Abs(axis) - g * t) * Mathf.Sign(axis);
    }
}