- Home /
Change light.color over time, repeat
Hi,
I've already found some bits of code here but that still doesn't answer my question. I'm trying to create a day/night cycle. I have 4 colours for each day time. So morning, noon, dusk, night. They are stored in separate variables.
I want the light colour to lerp from one to another in certain amount of time per colour. Can someone write a bit of code and hopefully describe WHY and WHAT'S happening? :)
tl;dr I want to cycle between 4 light colours to create a day/night cycle. Also, how do I loop this behaviour?
You could look at Fade for a starting point, specifically the third Colors function. The only thing you'd have to add is a certain amount of time for each color.
Answer by CanOfColliders · Feb 27, 2012 at 02:10 PM
hm, still at work and can't actually test this right now to write it, but take a look at http://www.youtube.com/watch?v=zxIyZzieFqE this covers a day / night cycle setup, including time and looping. It's not actually blending colors but uses a moving sun/moon & blending skyboxes but hope that helps
Answer by Rs · Sep 25, 2012 at 05:16 PM
Hello, here's my example. I do not use lerp here but if you need that you can substitute the Mathf.Sin with any functions you want. Some code might be redundant to you since I included some timing optimization. The light is updated every t you specify in UPDATE_INTERVAL. The rest should be straight forward. Hope it helps.
Note: you might need more precise functions to map the sun colour on time. These were just OK for my purposes.
using UnityEngine;
using System.Collections;
public class DayTimer : MonoBehaviour {
// Update every UPDATE_INTERVAL seconds
private const int SECONDS_IN_A_DAY = 86400;
// variables to do timing on the update (optimization)
private const float UPDATE_INTERVAL = 1f;
private float lastUpdateTime = 0f;
private System.DateTime now = System.DateTime.Now;
public bool manualControl = false;
public int currentDayTime = 0;
// Light that should be controlled by the DayTimer
public Light controlledLight01 = null;
// Use this for initialization
void Start () {
lastUpdateTime = Time.time;
}
void Update () {
if(manualControl) {
} else {
now = System.DateTime.Now;
currentDayTime = System.Convert.ToInt16(now.Hour)*3600 + System.Convert.ToInt16(now.Minute)*60 + System.Convert.ToInt16(now.Second);
}
// Update the light every UPDATE_INTERVAL seconds
if(Time.time > lastUpdateTime + UPDATE_INTERVAL) {
if(controlledLight01!=null) { // If we have a light
Light l = controlledLight01.GetComponent<Light>(); // Get the Light component
// Mathf.Sin doesn't convert automatically to float so the given values need to be converted manually
float currentDayTimeF = System.Convert.ToSingle(currentDayTime);
float secondsInADayF = System.Convert.ToSingle(SECONDS_IN_A_DAY);
Color c = new Color();
c.r=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f);
c.g=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f);
c.b=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f) * 0.3f;
c.a=1f;
l.color = c;
lastUpdateTime = Time.time;
}
}
}
}
Your answer
Follow this Question
Related Questions
Ambient light and lightmapping 1 Answer
How to remove elliptical color bands on textures 1 Answer
How to slowly change the colour of a directional light object 3 Answers
Day Night Cycle sun fade 1 Answer
Independent Ambient Light Shader 0 Answers