- Home /
The question is answered, right answer was accepted
Making a light that fades out one color and fades in a different color
As in start one color, fade out, fade in with a second color, fade out, and loop.
I'm new to Unity and kinda new to programming and very new to javascript, so I'm sure I'm doing this some weird way. I did some research but now I'm stuck. At one point I'd gotten it to fade out of the first color and fade in the second color but it would break after that.
If I run this script, It very quickly alternates between the two selected colors- obviously not the intended effect.
var color0 = Color.magenta; var color1 = Color.red; var duration = 1.0; var lastColor; var nowFading; var currentColorSum; var lastColorSum;
function Awake () { lastColor = color0; nowFading = true; }
function Update () {
lastColorSum = lastColor.r + lastColor.g + lastColor.b;
while(nowFading)
{
currentColorSum = light.color.r + light.color.g + light.color.b;
if(currentColorSum <= 0)
{
nowFading = false;
}
else
{
if (lastColor == color0)
{
light.color -= color0 / duration * Time.deltaTime;
}
else if (lastColor == color1)
{
light.color -= color1 / duration * Time.deltaTime;
}
else
{
Debug.LogError("lastColor invalid", this);
}
}
}
while(!nowFading)
{
currentColorSum = light.color.r + light.color.g + light.color.b;
if(currentColorSum >= lastColorSum)
{
if (lastColor == color0)
{
lastColor = color1;
nowFading = true;
}
else if (lastColor == color1)
{
lastColor = color0;
nowFading = true;
}
else
{
Debug.LogError("lastColor invalid", this);
}
}
else
{
if (lastColor == color0)
{
light.color += color1 / duration * Time.deltaTime;
}
else if (lastColor == color1)
{
light.color += color0 / duration * Time.deltaTime;
}
else
{
Debug.LogError("lastColor invalid", this);
}
}
}
}
How come it's not doing what I want? And am I using all of these lines of code to do something I could do with a simple math function?
Answer by Eric5h5 · May 07, 2010 at 07:45 PM
var color0 = Color.magenta; var color1 = Color.red; var duration = 1.0;
function Start () { while (true) { yield LerpLightColor (color0, Color.black); yield LerpLightColor (Color.black, color1); yield LerpLightColor (color1, Color.black); yield LerpLightColor (Color.black, color0); } }
function LerpLightColor (col1 : Color, col2 : Color) { var t = 0.0; var rate = 1.0/duration; while (t < 1.0) { t += Time.deltaTime * rate; light.color = Color.Lerp(col1, col2, t); yield; } }
Answer by Mike 3 · May 07, 2010 at 06:45 PM
this starts a coroutine which fades between each color once every second
var col1 = Color.blue; var col2 = Color.green; var light : Light; var duration : float = 1.0;
function Start() { ColorFunction(); }
function ColorFunction() {
var t = 1.0;
while(true)
{
light.color = Color.Lerp(col1, col2, Mathf.PingPong(t,1));
yield;
t += Time.deltaTime / duration;
}
}
Thank you for the very quick response.
This is actually the type of code I had started out with. The problem was I didn't want to get that sort of blending of colors effect that it gets when fading between colors. I wanted to modify it so that it would start at one color, fade all the way out (so that the light was "off"), and then fade in to a different color, and repeat that.
Sorry, I knew that would be confusing, I should've tried to be more clear.
Also, you don't have to declare light as a var since it is referencing the GameObject you're attached to. You actually can't run it that way
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Shader errors 0 Answers
Hardlight for spotlight 0 Answers
Script flares onto lights? 1 Answer