How can I change a float variable over time?

I have a simple script which changes a float variable in another script (via GetComponent) when an input is pressed. At the moment the variable changes instantly but I would like to be able to control the speed at which this float variable changes while the input is pressed. For eg. if I press the input for a short time the float starts to approach the value but only reaches it if the input is pressed for a long enough time. How can I do this? In the “if” statement if I change the “=” to a “-=” of “+=” it does change the float over time but I still can’t control how fast it changes.

using UnityEngine;
using System.Collections;

public class HydraulicSuspension : MonoBehaviour {
	
	public float lowRider = 0.1f;
	public CarDynamics carDynamics;
	
	//Use this before intialization
	void Awake(){
		carDynamics = GetComponent <CarDynamics>();				
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		
		//Check if Hydraulics is pressed
		if(Input.GetAxisRaw("Hydraulics") == 1) {
			carDynamics.suspensionTravelFront = lowRider;
			carDynamics.SetCarParams();
		}
	}
}

Take a look here:

You’ll probably end up with something like:

function FixedUpdate () {
    if(Input.GetAxisRaw("Hydraulics") == 1) {
        carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time);
        carDynamics.SetCarParams();
    }
}

…where min and max are float vars that you’ve set at the min and max range you want.

To control speed, you can do:

carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time * speed);

…where speed is another float var.

This is a useful script for controlling fire rate:

The first answer of this question solved everything for me. For anyone having trouble understanding how to set up a Mathf.lerp, I recommend you read this. It isn’t as straight forward as one would think and it took me a while to get it. It is basically what whydoidoit suggested previously but it goes into a few simple examples as well. Hope that helps.