- Home /
Randomly Transition Camera Background Color?
This is really just a cute detail I was going to throw on my camera quickly, but it turned into a hurdle - conceptually what I want to do is really simple, but I'm missing something. With the code below, my BG just goes to white and stays there, even though the debug log shows valid RGBA values looping through. I've tried both lerping and straight setting the camera Bg color (as commented out below)
Any ideas what I'm getting wrong here?
var bgColor : Color;
var blooping : boolean = true;
function Start () {
StartCoroutine(bgColorShifter());
}
function bgColorShifter(){
while (blooping){
bgColor.r = Random.Range(0,255);
bgColor.g = Random.Range(0,255);
bgColor.b = Random.Range(0,255);
bgColor.a = Random.Range(0,255);
Debug.Log("bgColor: "+bgColor);
var currentColor = Camera.main.backgroundColor;
//Camera.main.backgroundColor = bgColor;
Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, 0.1);
yield WaitForSeconds(1.0);
}
}
Just like to point out that, even when your values are in the range of 0.0 to 1.0, you change the "target-color" each iteration and you just lerp 10%
towards this color each time. This seems to be a little bit strange. Even if you want a real random color, i guess you want to lerp slowly over time to that color, right?
Answer by Berenger · Jun 26, 2012 at 01:47 AM
The color of the background should be updated, unless you're not actually seeing the background but an object in front of the camera.
Anyway, you're code isn't updating correctly. Every second, you pick a new random color (note that Color are between 0 and 1, your random will only create colors like (0,1,0,0) or (1,1,0,1). Then you change the background to 90% of that new color and 10% of the previous color. You should try this :
var bgColor : Color;
var blooping : boolean = true;
function Start () {
StartCoroutine(bgColorShifter());
}
function bgColorShifter()
{
while (blooping)
{
bgColor.r = Random.value; // value is already between 0 and 1
bgColor.g = Random.value;
bgColor.b = Random.value;
bgColor.a = 1.0; // I don't think alpha matters
Debug.Log("bgColor: "+bgColor);
var t: float = 0f
var currentColor = Camera.main.backgroundColor;
while( t < 1.0 )
{
Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
yield null; // Wait one frame
t += Time.deltaTime;
}
}
}
Awesome - this worked great. $$anonymous$$inor thing, though - "yield null;" throws me a strange compiler error in $$anonymous$$onodevelop. I used waitForseconds() anyway, since that let me control how gradual the colors shift, but I wanted to mention it since I didn't understand the compiler error and haven't seen that use of yield before.
I use javascript only here, but I don't think there should be an error. Anyway, if it works for you ^^
Answer by hathol · Jun 26, 2012 at 01:45 AM
From the script reference: "Each color component is a floating point value with a range from 0 to 1." So just use Random.value instead of Random.Range() and you should be fine :)