I have been working in an easy day/night cycle for a game I'm doing. I found some scripts allowing to implement it, but in my opinion, the more simple and straightfoward a program is, the easier to change and tune it. So this system takes shortcut and goes directly to what matters: a day/night cycle that can look realistic enough. This is just a part of what I made for the game, but it is released as an independent (and free) package that can be imported easily. Right now it covers the basic: sun and moon orbits. Though it's possible I'll add additional features as weather, athmospheric effects, shader changes to make the landscape more 'monochrome' in low light conditions, or additional moons. Those ones are still in testing phase, though. I'm a newbie in Unity and this is my first try. Any advice is very welcome. DOWNLOAD https://dl.dropboxusercontent.com/u/214626438/DayNightCycle.unitypackage HOW IT LOOKS Well, you can take a look to the game I'm doing http://forum.unity3d.com/threads/200411-Mountains HOW IT WORKS The basic process is: 1. There's a sphere gameobject called "SunSphere" that contains all celestial bodies (sun, moons). Those bodies are placed as children in this gameobject. Once they're placed, they don't move anymore. Instead of it, the sphere rotates around its center. 2. SunSphere objects contains a script called "SunSphere" that controls its behaviour. Here different features can be placed. - MaximumSunIntensity and MaximumMoonIntensity control how strong are sun light and moon light. This maximum intensity happens when the body is at the top of the sky. The lower they are, the weaker the light becomes. - Moonphase degree controls... well, moon phase. 0 for new moon, 180 for full moon. - AngleNorthDegrees controls where is the North in your map. - Declination and WinterVsSummer control how is the cycle. Declination ranges from 0 to 90 degrees. 0 correspond to a perfect spring or fall solstice in the equator, with the sun in the perpendicular of earth. The far you moves from equator toward poles, and the more you get close to winter or summer solstice, the bigger the declination. In the Artic Circle in winter or summer solstice, declination would be 90 degrees. 30 to 60 degrees are more standard values. To increase or decrease duration of days you can use WinterVsSummer, ranging from -0.5 to 0.5. The higher, the longer days are, the lower, the shorter. - Finally, Hour the sun is at the top is used to know which time corresponds to solar noon. Usually it doesn't concur with 12h. It uses to be one or two hour later. Standard values are 13 or 14. NOTE SunSphere has a grid rendered to make it visible. Of course, this shouldn't be rendered in a actual game! 3. SunTarget should be placed in the center of your map. It's used to set the direction both for sun light and moon light. NOTE Be aware that there's a 3rd light, MoonIllumination, that comes from the sun and only affects moon. It's used to simulate the different phases of moon. 4. The moon is formed by a rendered sphere with a moon picture texture. Moon light, that lights earth, is another object children from the previous one. The sun is built the opposite way: the light is in the parent, the rendered body in the children. Why is that? Well, what matters from sun is his light, what matter from moon is his rendered shape in the sky. Though moonlight and sunbody are provided, as children they can be turnt off without affecting the former ones. 5. I added an additional GameTime object. It includes the singlenton pattern so there's not danger to duplicate it. It can control gametime in seconds, days, or (day, hour, minute, second) format, can be paused or fastforwarded, and it's used by the "SunSphere" object as time variable. Any doubt, just ask.
I will definitely give this a go in my game. Thank you for sharing and all credit goes to you if I end up keeping it.
Thanks so much for this, it's pretty awesome. How was your skybox done? Busy running this on a tablet and seems to be running great for now.
Love it! It gets just a tid bit too dark at night. I would turn up the ambient light emitted from the scene just a tad when it hits night, and reset it to default during the day. One thing I did when developing my day/night cycle was use C# Messanger to Broadcast a message to all of my Fire particle effects whenever it turned to night. I believe it was at about 7:00 PM all of the torches I had lining my roads would turn on their particle effects. At around 5:30-6:00 AM I would broadcast a message to turn them all off since they are no longer needed. That was just me, though, since I didn't feel like messing with the scenes ambient lighting, I just provided them with another light source. The way you do it is, of course, up to you. ^_^
It works, except it's not getting dark, I cleared my light map, shouldn't it get dark when the sun is below the terrain?
It should get dark when the sun is pointing away from the terrain. When the sun is below the terrain, I believe light can still shine through since the bottom of the terrain doesn't have any bottom faces or a collider.
I put a sunblocker plane under my terrain, no effect, maybe I have too much ambient? I heard you can't have lightmapped terrain with a moving dynamic light, the lightmapping never changes. I got rid of the lightmap on this terrain. It looks cool anyway, thanks, Tamaroq!
Here's a build of it, 23MB, left-click shoots, Alt-F4 (Windows) quits. Later I turned up the ambient, looks better now. Castle, containters by Daniel74, Viking ship by Tigertowel, both from Blendswap https://dl.dropboxusercontent.com/u/102638093/arpg_combat.zip
Hey guys, fantastic job, iv been using this with a good deal of success, only problems iv had are, 1. sun wasn't rendering so i added a flare to the sunbody object 2. moon is not rendering, and i havent found a solution to this problem If anyone could help with my second issue id be very greatfull cheers guys and thankyou again to the creator, fantastic job.
Hmm, Maybe you could make a flare object, that just looks like a moon, so similarly to the sun there will be a flare on your invisible moon, it just won't look like it's a flare.
I'd instantiate a sphere over my light, give it some sort of self-illumination shader, and slap a moon texture over it. Child the moon object to the light that is emitting the moons light, and it should work pretty well. Not sure, though. Never actually made a moon.
I don't get the time business. No matter what I put in 'set day length in seconds', 1440 or 14400, the day lasts 1 minute or so.
Now I get it, set day length to 0, and time scale to 200, looks like the day-night will last about 5 minutes. I wonder how to get the skybox to go dark at night. Burgzerg had 2 different skyboxes- a day one and a night one they'd switch at dawn and dusk.
I put this into a scene with a simple terrain. There is no night cycle. As soon as the sun sets, it rises again. Any help?
Uh, I need to know how to make the clouds and stuff move and how to make the skybox dark at night. Help?