- Home /
Question by
LokiFirebringer · Dec 11, 2011 at 06:32 AM ·
javascriptlightingcolorchange
Switching color of light
I am rather new to Unity, and I was wondering how to smoothly change the color of a light in the Update function. I have tried a few variations of code based on Color.Lerp, but whenever I would, my colors would shoot up to an extremely high number. I don't have that code on me, but it was along the lines of:
light.color = Color.Lerp(colorA,colorB,Update)
colorA and colorB are your basic colors, and Update ranged from Time.time to Time.deltaTime mixed with other variables to try to get increment from 0 to 1.
What can I do to get the colors to fade from one to the other?
Comment
Answer by Daniel 6 · Dec 11, 2011 at 07:24 AM
Here's one approach. I haven't compiled the code so not sure if it contains spelling errors.
var colorA : Color;
var colorB : Color;
function DoLerp (duration : float) {
var startTime = Time.time;
while (Time.time - startTime < duration) {
var amount = (Time.time - startTime) / duration;
light.color = Color.Lerp (colorA, colorB, amount);
yield;
}
light.color = colorB;
}
@script RequireComponent (Light)