Continuous Random Color With Objects in Processing

Search Unity

Unity ID

A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate in the Unity community.

  1. 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.

  2. To get a smooth transition you need to recalculate the color each frame. Best place to do that would be in Update().
    1. if (timeLeft <= Time. deltaTime )
    2. // assign the target color
    3.     renderer. material . color = targetColor;
    4. // start a new transition
    5.     targetColor = new Color(Random. value, Random. value, Random. value ) ;
    6. // transition in progress
    7. // calculate interpolated color
    8.     renderer. material . color = Color. Lerp (renderer. material . color, targetColor, Time. deltaTime / timeLeft) ;
    9.     timeLeft -= Time. deltaTime ;
  3. works like a charm, great.

    thanks

  4. 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

neeseyestand.blogspot.com

Source: https://forum.unity.com/threads/smooth-continuous-color-change-over-the-game.228223/

Belum ada Komentar untuk "Continuous Random Color With Objects in Processing"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel