Why do functions stop when I release a button?

Hi, I have a fairly simple function being executed when I press and de-press a button.

Basically I have it to do this on a button press:-

t += Time.deltaTime * accelerationSpeed;

(accelerationSpeed is just a float).

And to do this when i let off the button:-

t -= Time.deltaTime * accelerationSpeed;

Its a simple up and down lerp.

My problem is when I let my finger off the button, this doesn’t continue to lerp down.

t -= Time.deltaTime * accelerationSpeed;

It doesn’t want to finish it’s job.

Any ideas would be greatly appreciated.

Cheers,Tim.

(Imagine revving an engine, it lerps up to 8000RPM, you let your foot off and it automatically lerps down RPM to the minimum,

That’s what my script is doing, but when i let my foot off, it stays at the RPM my foot was at.)

EDIT: added code

var minimum : float  = 0.0;
var maximum : float = 2003;
var accelerationSpeed : float = 0;
var t:float = 0.0;
static var CurrentRPM : float = 0; 
var AccelPedal : float = 0; 

function Update (){

    for (var touch : Touch in Input.touches)
    {
    
        if (touch.phase  == TouchPhase.Stationary && ThrottleButton.HitTest (touch.position)) { 
            AccelPedal= 1;
            LerpAccelup ();
            ThrottleButton.color = Changecol;
            
            }
            
            
           else if (touch.phase  == TouchPhase.Ended && ThrottleButton.HitTest) { 
            AccelPedal= 0;
            ThrottleButton.color = Changecol2;
            LerpAccelDown ();
            
            CurrentRPM = Mathf.Lerp (minimum , maximum, t);		
            t = Mathf.Clamp01(t);


                        
     }

	BackLeftWheel.motorTorque = CurrentRPM * AccelPedal;
	BackRightWheel.motorTorque = CurrentRPM * AccelPedal;

}


function  LerpAccelup (){
    t += Time.deltaTime * accelerationSpeed;
    

 }
 
 function  LerpAccelDown (){  /////Here is where it stops
 
     t -= Time.deltaTime * accelerationSpeed ;

    
}

I also tried:

 else if (touch.phase  == TouchPhase.Ended && ThrottleButton.HitTest) { 
                AccelPedal= 0;
                ThrottleButton.color = Changecol2;
                 t -= Time.deltaTime * accelerationSpeed ;

When you’re posting a code, please post a full snippet. In this case with code handling button press and release.

Most probably you just execute your speed decreasing code only once, while it should continue in next frames. You can do this the following way:

int _accelerationFactor = -1;
float _minRpm = 1000f;
float _maxRpm = 8000f;

public float rpm;
public float accelerationSpeed;

void Start ()
{
    rpm = _minRpm;	
}

void Update ()
{
    if (Input.GetKey(KeyCode.Space))
    {
        _accelerationFactor = 1;
    }
    if (Input.GetKeyUp(KeyCode.Space))
    {
        _accelerationFactor = -1;
    }

    rpm = Mathf.Clamp(rpm + Time.deltaTime * accelerationSpeed * _accelerationFactor, _minRpm, _maxRpm);
}

When pressing space, rpm will grow up to maxRpm, when you release it, it will decrease until reaches minRpm. You can of course optimize if further, e.g. by not executing Mathf.Clamp line if key is pressed and rpm is already at maximum, or key is released, while rpm is already at minimum.

Your loop handles all touches but when you release your finger, there are no touches anymore so it won’t enter the loop and thus it can’t execute the else if. You have to place the up-condition next to the loop:

for (var touch : Touch in Input.touches)
{
     ...
}

if(Input.touchCount == 0)
{
    AccelPedal= 0;
    ThrottleButton.color = Changecol2;
    LerpAccelDown ();

    CurrentRPM = Mathf.Lerp (minimum , maximum, t);     
    t = Mathf.Clamp01(t);
}