- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi everyone,
For my company's Unity 5.1 game project, we developed a full day and night cycle with an orbiting sun and moon, sunrise and sunset effects and a color-changing sky that slowly graduates from night-time to dawn to midday and back to night-time. We are freely sharing the full script for this sky change below.
The sky change was achieved by placing a JavaScript script inside the main camera that runs when the game begins and loops constantly from night to day to night, using color palette mixing formulas to slowly phase from one color to another. A 24 hour cycle in the game takes approximately 45 minutes of real time from start to finish, about the same as a day in 'Grand Theft Auto V'.
The length of the cycle is determined by the variable name 'Duration' To achieve a 45 minute in-game day, we defined the duration to be 1215 seconds. To increase or decrease the length of your own day, you can simply increase or decrease this value to accelerate or reduce the speed of the color phasing.
Here is the full JavaScript to place inside your main camera..
*******
function Update () { // ping-pong animate background color var color1 : Color = Color.blue * Color.clear - Color.black + Color (.3,.4,.6); var color2 : Color = Color.cyan * Color. white + Color (.3,.4,.6); var duration = 1215.0; // Set clear flags to color GetComponent.<Camera>().clearFlags = CameraClearFlags.SolidColor; var t : float = Mathf.PingPong (Time.time, duration) / duration; GetComponent.<Camera>().backgroundColor = Color.Lerp (color1, color2, t); }
To help you to mix your own unique color formula, here's a link to Unity's documentation for the 'Color' feature.
http://docs.unity3d.com/ScriptReference/Color.html
Also note that if you have more than one camera in your program and switch between them, put the script in each camera so that they all start their color cycle at the same time and so the sky is consistent when you switch from one camera to another.
Good luck!
Edit: here is a sample of the dusk-to-dawn-to-dusk color change cycle from our own project.
Link Copied

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page