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

Color.lerp is not working

Discussion in 'Scripting' started by chewdri, Dec 14, 2014.

  1. chewdri

    chewdri

    Joined:
    May 1, 2013
    Posts:
    22
    Hello, I'm trying to use the Color.lerp in my project but it doesnt works and I have no idea why.

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. public class ambient_light : MonoBehaviour {
    4.  
    5.     Color color1;
    6.     Color color2;
    7.     Color color3;
    8.  
    9.     public float intervalle_trans_color1;
    10.     private float m_nextProc_color1;
    11.    
    12.     public float intervalle_trans_color2;
    13.     private float m_nextProc_color2;
    14.    
    15.     public float intervalle_trans_color3;
    16.     private float m_nextProc_color3;
    17.  
    18.     public bool trans1;
    19.     public bool trans2;
    20.     public bool trans3;
    21.  
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.  
    27.         color1 = new Color(0.25f,0.25f,0.5f);
    28.         color2 = new Color(0.25f,0.5f,0.25f);
    29.         color3 = new Color(0.5f,0.25f,0.25f);
    30.  
    31.         intervalle_trans_color1 = 6;
    32.         intervalle_trans_color2 = 6;
    33.         intervalle_trans_color3 = 6;
    34.  
    35.         m_nextProc_color1 = 0;
    36.         m_nextProc_color2 = 2;
    37.         m_nextProc_color3 = 4;
    38.  
    39.         RenderSettings.ambientLight = color1;
    40.    
    41.     }
    42.    
    43.     // Update is called once per frame
    44.     void Update () {
    45.  
    46.  
    47.         if (trans1 == true) {
    48.  
    49.             RenderSettings.ambientLight = Color.Lerp (color1, color2, Time.time);
    50.  
    51.         }
    52.  
    53.         if (trans2 == true) {
    54.  
    55.  
    56.             RenderSettings.ambientLight = Color.Lerp (color2, color3, Time.time);
    57.  
    58.            
    59.         }
    60.  
    61.         if (trans3 == true) {
    62.  
    63.             RenderSettings.ambientLight = Color.Lerp (color3, color1,  Time.time);
    64.            
    65.         }
    66.        
    67.  
    68.         if (Time.timeSinceLevelLoad >= m_nextProc_color1 )
    69.         {
    70.             m_nextProc_color1 += intervalle_trans_color1;
    71.             trans1 = true;
    72.             trans3 = false;
    73.         }
    74.  
    75.         if (Time.timeSinceLevelLoad  >= m_nextProc_color2 )
    76.         {
    77.             m_nextProc_color2 += intervalle_trans_color2;
    78.             trans2 = true;
    79.             trans1 = false;
    80.            
    81.         }
    82.  
    83.         if (Time.timeSinceLevelLoad  >= m_nextProc_color3 )
    84.         {
    85.             m_nextProc_color3 += intervalle_trans_color3;
    86.             trans3 = true;
    87.             trans2 = false;
    88.            
    89.         }
    90.  
    91.    
    92.     }
    93. }

    I'd like to have a loop of smooth lerp between 3 colors on the ambientLight, with this code the color is "lerp" only the first time, after that it just switch from one color to the other without "lerp".
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. chewdri

    chewdri

    Joined:
    May 1, 2013
    Posts:
    22
    I have already read this, I tried to replace Time.time by many things (like 1 * Time.deltatime) but it didnt works better.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    In that case you should know that using Time.time doesn't work.
    Instead you may store the time in an additional variable (transStart) when the animation is started (e.g. trans1 set to true). You can then use (Time.time - transStart).
     
  5. chewdri

    chewdri

    Joined:
    May 1, 2013
    Posts:
    22
    Thanks !