- Home /
Color Lerp Problems
Hey there Unity, having some strange troubles with my color lerping.
The SlerpToBlack() function runs as expected, but as soon as it gets done and hits color.black, everything pops back to white, and it all stops.
Maybe I'm going about this the wrong way? I've commented out other color sources, but they are affected the same way:
public class DayAndNight : MonoBehaviour {
public Color directionalLightStarting;
//public Color fogStarting;
//private Color ambientLightColor;
private Color directionalLightColor;
//private Color fogColor;
//public Color ambientLightStarting;
public Light mainDirectionalLight;
public bool slerpToBlack = true;
public bool slerpToWhite = false;
public float speed = 0.05f;
// Use this for initialization
void Start () {
mainDirectionalLight = GetComponent<Light> ();
}
void Update(){
if (slerpToBlack) {
SlerpToBlack ();
}
if (slerpToWhite) {
SlerpToWhite();
}
}
public void SlerpToBlack(){
//ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
//fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, Time.time * speed);
mainDirectionalLight.color = directionalLightColor;
//RenderSettings.ambientLight = ambientLightColor;
//RenderSettings.fogColor = fogColor;
if (mainDirectionalLight.color == Color.black) {
slerpToBlack = false;
slerpToWhite = true;
}
}
public void SlerpToWhite(){
//ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
//fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, Time.time * speed);
mainDirectionalLight.color = directionalLightColor;
//RenderSettings.ambientLight = ambientLightColor;
//RenderSettings.fogColor = fogColor;
if (mainDirectionalLight.color == directionalLightStarting) {
slerpToWhite = false;
slerpToBlack = true;
}
}
}
Answer by NoseKills · Dec 28, 2014 at 01:55 AM
Because of what Lerp does, as soon as Time.time * speed
equals or exceeds 1, you start doing both SlerpToBlack()
and SlerpToWhite()
every time in Update.
After the third parameter in Color.Lerp
becomes 1, it always returns the target color. That causes you to go inside the if (mainDirectionalLight.color == Color.black)
in SlerpToBlack()
and because you have 2 if's instead of an if-else in your Update()
you go straight into SlerpToWhite()
in the same Update
cycle, and again (still) the third parameter is 1 so you go inside the if()
in both methods all the time.
This for example should fix it
public class DayAndNight : MonoBehaviour {
public Color directionalLightStarting;
//public Color fogStarting;
//private Color ambientLightColor;
private Color directionalLightColor;
//private Color fogColor;
//public Color ambientLightStarting;
public Light mainDirectionalLight;
public bool nightIsComing = true;
public bool slerpToWhite = false;
public float speed = 0.3f;
public float lightTimer;
public float nightDuration = 2f;
public float nightTimer = 0f;
// Use this for initialization
void Start ()
{
mainDirectionalLight = GetComponent<Light>();
}
void Update()
{
if (nightTimer > 0)
{
nightTimer -= Time.deltaTime;
}
else
{
lightTimer += Time.deltaTime * speed;
if (nightIsComing)
{
SlerpToBlack ();
}
else
{
SlerpToWhite();
}
}
}
public void SlerpToBlack(){
//ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
//fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, lightTimer * speed);
mainDirectionalLight.color = directionalLightColor;
//RenderSettings.ambientLight = ambientLightColor;
//RenderSettings.fogColor = fogColor;
if (mainDirectionalLight.color == Color.black) {
nightIsComing = !nightIsComing;
nightTimer = nightDuration;
lightTimer = 0;
}
}
public void SlerpToWhite(){
//ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
//fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, lightTimer * speed);
mainDirectionalLight.color = directionalLightColor;
//RenderSettings.ambientLight = ambientLightColor;
//RenderSettings.fogColor = fogColor;
if (mainDirectionalLight.color == directionalLightStarting) {
nightIsComing = !nightIsComing;
lightTimer = 0;
}
}
}
Works perfectly! Thanks so much for your help. Never knew color.lerp worked like this.
Could you perhaps explain how to keep it night for a defined amount of time?
Please click the checkmark next to the answer to mark this question as answered if you think the original problem is solved.
I edited the answer to have a variable for night duration.
If you want to make the effect even more natural looking, you should probably use $$anonymous$$athf.Sin()
to do the Lerping, but that's a topic for another Question...
I'm very interested in learning about how I could make it more realistic! Thank you for your responses, they've been very helpful.
AGAIN : Please click the check mark next to the answer to mark this question as answered.
That's how Unity Answers works. Otherwise this question will show as "unanswered" and people will waste time trying to solve a solved question
You're welcome
Your answer
Follow this Question
Related Questions
Color LERP problem 1 Answer
Color.lerp help (GUI.color) 1 Answer
Spawned Object Gradient Mesh Colors? 2 Answers
Can you change Alpha and Blue(rbg) simultaneously? 3 Answers
smoothly switch between 2 colors that are using lerp 2 Answers