x


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?

more ▼

asked Feb 23 '12 at 03:43 PM

tonny gravatar image

tonny
1 1 1 1

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.

Feb 27 '12 at 04:59 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
         }
       }

    }
}
more ▼

answered Sep 25 '12 at 05:16 PM

Rs gravatar image

Rs
1 3

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 27 '12 at 02:10 PM

CanOfColliders gravatar image

CanOfColliders
51 1 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x427
x36
x28
x19

asked: Feb 23 '12 at 03:43 PM

Seen: 942 times

Last Updated: Sep 25 '12 at 05:16 PM