Is there a way to smoothly transition between two floats, with it slowing down towards the end

So I am making a helicopter sim in Unity, I was wondering, is there a way to smoothly transition between two floats,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Helicopter : MonoBehaviour
{
    public bool engineOn;

    public Rigidbody rb;

    public float windDrag;

    public float upForce;
    public float gravity;

    public Animator animator;

    public Transform rotor;

    public float pitch;

    void Start()
    {
        rb.drag = windDrag;
    }

    void Update()
    {
        transform.rotation = Quaternion.Euler(pitch, 0f, 0f);

        if (Input.GetKeyDown(KeyCode.E))
        {
            engineOn = !engineOn;
        }

        if (engineOn)
        {
            if (Input.GetKey(KeyCode.Space))
            {
                upForce += 40;
            }
            else if (!Input.GetKey(KeyCode.Space) && upForce > 0)
                upForce -= 30;

            if (Input.GetKey(KeyCode.W))
            {
                float addedPitch = pitch -= .3f;
                pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
            }

            if (Input.GetKey(KeyCode.S))
            {
                float addedPitch = pitch += .3f;
                pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
            }

            
        }

        rb.AddForce(rotor.up * upForce);

        SetBooleans();
    }

    void SetBooleans()
    {
        animator.SetBool("EngineOn", engineOn);
    }
}

Here I wanna transition between the pitch and addedPitch variable, and kind of want it to go that when I when I hold down the W key, the pitch slowly goes up and starts to speed if I hold it long enough, and when I release W key, the incrementation of pitch slows down and eventually comes to a halt, where pitch is static. Mathf.Lerp didnt work well for me. Please help.

There are a few issues with this part here (and the same goes for the same code on ‘S’)

float addedPitch = pitch -= .3f;
pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);

The first line takes pitch, reduces it by .3 then sets addedPitch to be the SAME value.
So if pitch is 15, we reduce it to 14.7 then set added pitch to also be 14.7.

Given that pitch and added pitch are the same, when we lerp between them we’ll always get that same value therefore the lerp is useless.

Also another note is that since you’re passing Time.deltaTime which is always 1 divided by framerate, given that the lerp fraction should be between zero and one, it’s always going to be effectively zero.

Now for the fix: If W and S just need to increase and decrease the pitch then you’ll probably want to specify a maximum, minimum and change speed.

Now you can call:

if (Input.GetKey(KeyCode.W))
{
    pitch -= pitchChangeSpeed * Time.deltaTime;
    pitch = Mathf.Clamp(pitch, minimumPitch, maximumPitch);
}

Then same for S except changing -= to +=.

Also I’ve noticed that you don’t use the pitch value anywhere within this code so unless you have another script that uses pitch then it won’t work because it’s never used!

Let me know how you get on!