Thursday, March 14, 2013

We have Day and Night!

Today, I worked on getting a day and night cycle running for my game(I like working on visual things first, it is more rewarding!). I am pretty happy with the outcome. I will go into more detail below but here is a brief summary.

There are two scripts used for the day night cycle. One controls the sun's properties and the other controls the time of day and rotation of the sun. The in-game day can be however long I want it to be, it is changeable. I can keep track of how many in-game days, months, and years have passed. The sun's rotation speed depends on how long the day is. I also added a Skybox that changes depending on the time of day. The time of day, for now, starts at 12pm and lasts for 30 minutes.

Detailed(With Pictures!)
Ok, so I started out writing a general script to keep track of time and to rotate a direction light in Unity. I use Time.deltaTime and store it in a variable and use that to keep track of days, months, and years. I use a simple Transform rotation to rotate the direction light on the x-axis. At this point the light rotates around the scene like an actual sun.

Fig 1: You can see the direction light in the upper right-hand corner. It is rotation and projecting the light flare on the lower left-hand corner

I wanted to have a more detailed system so I continued coding. I wanted to change the brightness of the sun every day and I wanted to add a Skybox.

To get different brightness settings every day, I created a script for the direction light. The script sets random values every day. The Game Time script I wrote earlier uses these values and adjusts the sun's brightness based on the time of day. For example, at noon or 12pm, the light is the brightest and as the day goes on it gets less bright. I really liked how this turned out and as little as the random values of brightness change I like the added effect.
Fig 2: This is just the direction light(sun) setting.

The Skybox at first was just one scene with clouds. I did not like it so I went online and found a new shader that allows me to add two scenes. One scene is a night sky and the other is a day sky. Using this new shader, I code in the Game Time script how it should blend the two scenes by time of day. I really liked how this turned out. I think it is beautiful! I feel like the world is coming to life, even though there is no world, yet!
Fig 3: The day scene skybox. The cube shown is to look at shadows.

Fig 4: The night scene Skybox. The cube shown is to look at shadows.

Side Note:
I added a moon with the same functionality as the sun but I did not like it so I did not keep it in my final setup. I like the night sky too much. Also, with my Game Time script I can add events depending on the time of day!

I will try to keep my thoughts organized on these posts. I cannot promise though. If anyone has questions, ask away!

No comments:

Post a Comment