Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Lerping colour back to start

Discussion in 'Scripting' started by melonhead, Apr 4, 2023.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    trying to lerp material colour from start to end only while holding mouse key so if button up before lerp complete the colour reverses back from its current colour, tried using GetColor to feed into the start of the reverse lerp but just starts faded out, i cant get the current colour to put back into the start of the reverse lerp, how to store the current colour during the forward lerp then use that to reverse from if lerp not complete

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class reverseSFADER : MonoBehaviour {
    6.  
    7. Material thisMat;
    8.  
    9. Color startColor = new Color32(255, 255, 0, 0); // 100
    10. Color endColor = new Color32(255, 255, 0, 50); // 100
    11.  
    12.  
    13.  
    14. Color currentColor;
    15.  
    16. float duration = 2; // How Many Seconds To Take Lerp
    17. private float t = 0; // Lerp Variable
    18.  
    19.  
    20.    void Start () {
    21.     thisMat = GetComponent<Renderer>().material;
    22.  
    23.  
    24.  
    25.      }
    26.  
    27.    
    28. void  Update (){
    29.  
    30. if (Input.GetKey(KeyCode.Mouse0)){
    31.  
    32.  
    33.      thisMat.SetColor("_TintColor", Color.Lerp(startColor, endColor,t));
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.     if (t < 1){
    42.      t += Time.deltaTime/duration;
    43.     }
    44.    
    45. currentColor=thisMat.GetColor("_TintColor");
    46. }else{
    47.            
    48.  
    49.              thisMat.SetColor("_TintColor", Color.Lerp(currentColor, startColor,t));
    50.  
    51.  
    52.  
    53.     if (t > 0){
    54.      t -= Time.deltaTime/duration;
    55.     }
    56.  
    57. }
    58.  
    59.      Debug.Log(t); // So Can See Time In Console
    60. }
    61. }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    What on earth is going on with your code formating? Did you throw it into the tumble drier before you posted it!

    Since you're walking the t-value backwards, you should not be reversing the order of start/end! You also don't want to grab currentColor and move from that, since that'll slow down things.

    Simply have your else block do:

    Code (csharp):
    1.  
    2. else {
    3.     if (t > 0){
    4.          t -= Time.deltaTime/duration;
    5.     }
    6.    
    7.     thisMat.SetColor("_TintColor", Color.Lerp(startColor, endColor, t));
    8. }


    The galant way of doing it is to split the "find the correct t" out of the code, and then just have the rest be the same:

    Code (csharp):
    1. void Update() {
    2.      var target_t = Input.GetMouseButton(0) ? 1f : 0f;
    3.  
    4.      t = Mathf.MoveTowards(t, target_t, Time.deltaTime / duration);
    5.      thisMat.SetColor("_TintColor", Color.Lerp(startColor, endColor, t));
    6. }
    A lot nicer to read!
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    In your case you would track the third control argument passed to Color.Lerp(): you would set desired / current to 0.0 and when you wanted the other color, set desired to 1.0 and let current move towards it, using Color.Lerp() to get the color.

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    Another approach would be to use a tweening package line LeanTween, DOTween or iTween.

    OR... just make a simple canned animation... No code needed!
     
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    sorry for the formatting my bad,
    very neat, thanks
     
  5. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    thanks will check out the code.