Search Unity

Smooth continuous color change over the game?

Discussion in 'Scripting' started by silentbullet, Feb 13, 2014.

Thread Status:
Not open for further replies.
  1. silentbullet

    silentbullet

    Joined:
    Jan 1, 2014
    Posts:
    2
    The script is assigning a random color to an object over the game, but the transition is not smooth. How can i make the color transition smooth?

    void RandomColor()
    {
    oldColor = renderer.material.color;
    newColor = new Color(Random.value,Random.value,Random.value);
    renderer.material.color = Color.Lerp (oldColor,newColor,Time.fixedTime);
    }
    InvokeRepeating ("RandomColor", 1.0f, 1.0f);


    I would appreciate any help.
    Thank You.
     
    Cajjos likes this.
  2. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    To get a smooth transition you need to recalculate the color each frame. Best place to do that would be in Update().

    Code (csharp):
    1.  
    2. float timeLeft;
    3. Color targetColor;
    4.  
    5. void Update()
    6. {
    7.   if (timeLeft <= Time.deltaTime)
    8.   {
    9.     // transition complete
    10.     // assign the target color
    11.     renderer.material.color = targetColor;
    12.  
    13.     // start a new transition
    14.     targetColor = new Color(Random.value, Random.value, Random.value);
    15.     timeLeft = 1.0f;
    16.   }
    17.   else
    18.   {
    19.     // transition in progress
    20.     // calculate interpolated color
    21.     renderer.material.color = Color.Lerp(renderer.material.color, targetColor, Time.deltaTime / timeLeft);
    22.  
    23.     // update the timer
    24.     timeLeft -= Time.deltaTime;
    25.   }
    26. }
    27.  
     
  3. silentbullet

    silentbullet

    Joined:
    Jan 1, 2014
    Posts:
    2
    works like a charm, great.

    thanks
     
  4. Deleted User

    Deleted User

    Guest

    Why are you dividing (Time.deltaTime / timeLeft) instead of multiplying the two values, because from what i've seen some people when using Lerp they usually multiply by deltaTime to smooth it, im not complaining :p:p but i just want to understand it.
    Thanx
     
  5. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Above is a linear model, as in a model that contains no easing.

    Let's say we want to LERP from black to white (from 0 to 255 for all 3 color sliders) and we want to do so over 1 second.
    In our example, timeLeft is equal to 1f.

    Now let's say our dinosaur hardware dates from 1990 and we only get 4 frames per second.

    First iteration
    That function is cast once initially. Time.deltaTime is equal to 0.25f, because remember, we only have 4 FPS.

    Since we haven't subtracted to timeLeft yet, timeLeft is still equal to 1f.

    t = Time.deltaTime / timeLeft
    t = 0.25f (0.25f / 1f = 0.25f)

    We have interpolated a fourth of the way from 0f to 255f. That's 63.75f. We're now at some dark gray color.
    Notice that 63.75 is also, coincidently (or not so much), a fourth of 255f.

    Second iteration
    We have subtracted 0.25f to timeLeft.
    timeLeft is now equal to 0.75f.

    t = Time.deltaTime / timeLeft
    t = 0.33333f (0.25f / 0.75f)

    That's a third of the way. But since we are interpolating from the current color, rather than the initial, this isn't a third of the way from 0f to 255f. It is a third of the way from 63.75f and 255f.

    Since we aren't interpolating over 255 anymore. Let's see how much we're interpolating over.
    We said we are at 63.75f and we want to reach 255f. This means we are interpolating over 191.25f.

    A third of 191.25f is 63.75f. On the first iteration, we moved 63.75f forward. On this iteration, 63.75f, again. You can see a linear pattern.

    We are now at 127.5f (63.75f from the first iteration and 63.75f from the second iteration) out of the total 255f.
    This is a medium gray and also, coincidently (or not so much), half of 255f.

    Let's keep going for fun.

    Third iteration
    We have subtracted another 0.25f to timeLeft.
    timeLeft is now equal to 0.5f.

    Time.deltaTime, as usual, is equal to 0.25f since we said we had 4 FPS. Remember?

    t = Time.deltaTime / timeLeft
    t = 0.5f (0.25f / 0.5f)

    This time, we are interpolating over 127.5f (255f - 127.5f). Remember, we are interpolating from our actual color, which is currently 127.5f.

    Since t equals 0.5f on this iteration, it means we want the value halfway between 127.5f and 255f.
    This value is 191.25f.

    On the first iteration, we raised by 63.75f.
    On the second iteration, we raised by 63.75f
    On the third iteration, we raised by 63.75f.

    Every iteration raises equal increments.

    Fourth iteration
    Now, for the final iteration, the above poster has a condition that states that if timeLeft == time.deltaTime or less, then you just assign the final value. In our case it is. Since we again subtracted Time.deltaTime to timeLeft. timeLeft is now equal to 0.25f, same as deltaTime.

    Magic, a fourth and final increment of 63.75f.
     
    Last edited: Nov 29, 2017
    FlutterExp, Dnurr, ndbn and 3 others like this.
  6. TDRSyt

    TDRSyt

    Joined:
    Jan 19, 2024
    Posts:
    1
    do i need to change anything to the code
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
Thread Status:
Not open for further replies.