Search Unity

How can I reset and repeat a Mathf.Lerp?

Discussion in 'Scripting' started by Jeepster, Jun 18, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    This Mathf.Lerp in the Update function makes the objects materials alpha channel turn from .50f to the value of 0.0f over time. (It becomes transparent and then the game object is disabled)
    How do I make it so that it resets on awake? I mean how do I make it start over when the game object is enabled again?

    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.  
    5.    
    6.         time += 3f * Time.deltaTime;
    7.         Color c = rend.material.GetColor("_TintColor");
    8.         c.a = Mathf.Lerp(.50f, 0.0f, time);
    9.         rend.material.SetColor("_TintColor", c);
    10.  
    11.  
    12.  
    13.  
    14.     }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The Lerp(valueA, valueB, lerpValue) function is quite simple: it interpolates between valueA and valueB according to the lerpValue. If lerpValue is 0 or smaller, it returns valueA, if it is 1.0 or larger, it returns valueB, and if it is anything inbetween, it returns the weighted average between those two. For example, if lerpValue is 0.2, it returns 80% * valueA + 20% * valueB.

    Long Story short: you control the output value with the third value. This is currently time. Instead, use a Counter that you set to 0, and update by adding Time.deltaTime whenever you want to re-start the lerp.

    There are also Pingpong lerps availabe, so it may be highly beneficial if you re-read the lerp Manual pages, as you can save a lot of work if you use them well.
     
    Jeepster likes this.
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183

    YES! This!

    Define a variable for the timer such as
    float lerpTimer = 0f;
    variable and then increment it by
    lerpTimer += Time.deltaTime;
    Then plug that into the function
    Lerp(a,b,lerpTimer);
    .
    When you want to restart the loop, make sure the a and b values are good and then reset the counter!
    lerpTimer = 0f;
     
    Jeepster likes this.
  4. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Something like this:

    You call
    TimeReset();
    when you want to restart the Lerp

    Code (CSharp):
    1.  
    2.     private float time;
    3.  
    4.     private void Awake()
    5.     {
    6.         TimeReset();
    7.     }
    8.  
    9.     private void Update()
    10.     {
    11.         time += 3f * Time.deltaTime;
    12.         Color c = rend.material.GetColor("_TintColor");
    13.         c.a = Mathf.Lerp(.50f, 0.0f, time);
    14.         rend.material.SetColor("_TintColor", c);
    15.     }
    16.  
    17.     void TimeReset()
    18.     {
    19.         time = 0f;
    20.     }
    21.  
     
    Last edited: Jun 18, 2019
    Jeepster likes this.
  5. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    You'll also want the original color to be recorded and rendered when the gameobject becomes active again

    Something like this maybe?
    Code (CSharp):
    1.     private float time;
    2.     private readonly float lerpSpeed = 3f;
    3.  
    4.     Color originalColor;
    5.     Color fadedColor;
    6.  
    7.     private void Awake()
    8.     {
    9.         originalColor = rend.material.GetColor("_TintColor");
    10.  
    11.         LerpReset();
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         time += lerpSpeed * Time.deltaTime;
    17.         fadedColor = rend.material.GetColor("_TintColor");
    18.         fadedColor.a = Mathf.Lerp(.50f, 0.0f, time);
    19.         rend.material.SetColor("_TintColor", fadedColor);
    20.  
    21.  
    22.         if (Input.GetKeyDown("a")) LerpReset();
    23.            
    24.     }
    25.  
    26.     void LerpReset()
    27.     {
    28.         rend.material.SetColor("_TintColor", originalColor);
    29.         time = 0f;
    30.     }
     
    Jeepster likes this.
  6. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Thank you very much @EdGunther. That is exactly what I was looking for and thank you for the explanation as well. I'll definitely look more into the Lerp function as it can help with a lot of things. It works exactly as I wanted it to, and now I can change the "if" statement to react with the players health script when he gets shot. Thank you :)
     
  7. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Thank you for the explanation. Yes I will be looking over what lerp can do again and how it all works. The example from Ed down below makes a lot of sense and is in line with your explanation. Great example, help and motivation. Thank you :)