- Home /
How do I loop a Color32.Lerp?
I am trying to loop a smooth change in a random color. Basically, I want the camera's background color to smoothly change between a random color. Right now, the code below lerps the first loop, but then just jumps to the colors instead of being smooth. So, for some reason, the first random color change works, but all the ones after jump to the next without lerping. How could I fix this? Thank you ;) BTW, if you go to k a h o o t . i t, you can see in the background what I'm trying to achieve. :)
var currentColor : Color32 = Color.white;
var randomColor : Color32 = Color.white;
var lerped : Color32 = Color.white;
function Start() {
StartCoroutine(ChangeColor());
}
function Update() {
lerped = Color32.Lerp(currentColor, randomColor, Time.time / 1);
this.camera.backgroundColor = lerped;
}
function ChangeColor() {
while(true){
currentColor = this.camera.backgroundColor;
randomColor = Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 1);
yield WaitForSeconds(1);
}
}
Answer by robertbu · Nov 02, 2014 at 05:15 PM
The final value of Lerp() goes between 0.0 and 1.0, so you need to introduce a timer that you reset when the color changes or some other mechanism that puts the values in the correct range:
#pragma strict
var currentColor : Color32 = Color.white;
var randomColor : Color32 = Color.white;
var lerped : Color32 = Color.white;
var time = 1.0;
private var timer = 0.0;
function Start() {
StartCoroutine(ChangeColor());
}
function Update() {
timer += Time.deltaTime / time;
lerped = Color32.Lerp(currentColor, randomColor, timer);
camera.backgroundColor = lerped;
}
function ChangeColor() {
while(true){
currentColor = randomColor;
randomColor = Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 1);
timer = 0.0;
yield WaitForSeconds(time);
}
}
Answer by sysameca · Nov 02, 2014 at 05:52 PM
Guess i am too late, but here is how ill code it:
public class ColorChange : MonoBehaviour {
public float timeInterval = 1.0f;
public float speed = 2.0f;
void Start () {
StartCoroutine(ColorChangeRoutine());
}
private IEnumerator ColorChangeRoutine()
{
float timer = 0.0f;
Color32 color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
while (timer < timeInterval)
{
timer += Time.deltaTime * speed;
camera.backgroundColor = Color32.Lerp(camera.backgroundColor, color, timer);
yield return null;
}
// On finish recursively call the same routine
StartCoroutine(ColorChangeRoutine());
}
}
I was in the middle of the code when i saw there was already one :)
Thumbs up since this is cleaner. Could also do it the other way around and roll the random code into Update() and skip the coroutine. I decided to stay close to his original code.
Answer by Nebukam · Nov 02, 2014 at 04:59 PM
You're using 'Time.time/1', which is actually a constantly increasing value. As per unity doc : "This is the time in seconds since the start of the game", so instead you need deltaTime "The time in seconds it took to complete the last frame"
lerped = Color32.Lerp(currentColor, randomColor, Time.deltaTime);
Sorry forgot one line
lerped = Color32.Lerp(currentColor, randomColor, Time.deltaTime); currentColor = lerped;