how do you reset lerps?

Hi,

I have here a Float value that is lerped from “min-max” on a TouchPhase.Stationary,

when I enter the TouchPhase.Ended , its meant to lerp back to 0.

Instead what happens is it lerps up fine, but when i let off the button, it instantly goes to the desired value. (not lerping)

Then…if I re-enter the TouchPhase.Stationary , instead of the value being “minimum”
, it’s stuck at “maximum”.

Here’s what I have so far. (I have moved them to the function Update () to no avail.

var minimum : float  = 0;
var maximum : float = 2000;
var CurrentRPM : float = 0; 

function Update () {
 for (var touch : Touch in Input.touches)
    {
    
 if (touch.phase  == TouchPhase.Stationary && ThrottleButton.HitTest (touch.position)) { 
            LerpAccelup ();
      }

else if (touch.phase  == TouchPhase.Ended && ThrottleButton.HitTest) { 
            LerpAccelDown ();
                        
      }

function  LerpAccelup (){
   t += Time.deltaTime;
   CurrentRPM = Mathf.Lerp (minimum , maximum, t/3 );



 }

 
 function  LerpAccelDown (){
    t += Time.deltaTime;
    CurrentRPM = Mathf.Lerp (maximum, minimum, t/3);

 }

Thanks! Tim.

Mathf.Lerp (A : float, B : float, t : float) returns a value between A and B based on t, where t is between 0 and 1.

In order to properly use this, you need to take more care in tracking what the value of t is. Debug.Log(t) would be handy for this.

It seems like you are trying to manipulate the value of t such that it moves towards 1 when the player touches the screen, and move toward 0 when they let go. However, you are only ever increasing the value of t. So, it starts at 0 and then continually increases regardless of what the player is doing.

Based on your functions, this could work if you first use

t = 1 - t;

when you want to switch directions. This would only complicate things, though.

Instead, try using Mathf.Clamp01(t) in order to keep the value of t between 0 and 1, and decrease the value of t when you want to decelerate. Then, just Lerp between minimum and maximum regardless of whether you are accelerating or decelerating:

    var minimum : float = 0;
    var maximum : float = 2000;
    var accelerationSpeed : float = 0.3;
    var CurrentRPM : float = 0;
     
    function Update () {
    for (var touch : Touch in Input.touches)
    {
     
    if (touch.phase == TouchPhase.Stationary && ThrottleButton.HitTest (touch.position)) {
    t += Time.deltaTime * accelerationSpeed;
    }
     
    else if (touch.phase == TouchPhase.Ended && ThrottleButton.HitTest) {
    t -= Time.deltaTime * accelerationSpeed;
     
    }
    
    t = Mathf.Clamp01(t);
    CurrentRPM = Mathf.Lerp (minimum , maximum, t);}

Also, I noticed that you were dividing t by 3 in your lerp call. I assume that you were trying to slow the lerp. Instead, you should use a float variable to represent this acceleration speed and then use it to alter the amount added or subtracted from t (see above).

Hope this helps. -Joaquin