Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Day and Night Cycle

Discussion in 'Made With Unity' started by tamaroq, Sep 17, 2013.

  1. tamaroq

    tamaroq

    Joined:
    Sep 14, 2013
    Posts:
    39
    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. :D

    DOWNLOAD

    https://dl.dropboxusercontent.com/u/214626438/DayNightCycle.unitypackage

    HOW IT LOOKS

    Well, you can take a look to the game I'm doing :cool:

    http://forum.unity3d.com/threads/200411-Mountains



    HOW IT WORKS

    $01.jpg

    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.
     
    Last edited: Sep 17, 2013
  2. Andrew_atl

    Andrew_atl

    Joined:
    Sep 21, 2010
    Posts:
    103
    Awesome thanks!
     
  3. Traxdata

    Traxdata

    Joined:
    Feb 2, 2013
    Posts:
    2
    Really nice script - works perfectly :) Thanks man
     
  4. sallim

    sallim

    Joined:
    Mar 3, 2014
    Posts:
    1
    Thank you very much, much appreciated for a beginner like me. Thanks man =)
     
  5. Infinite_Gamer

    Infinite_Gamer

    Joined:
    Dec 6, 2013
    Posts:
    11
    Really nice!!
     
  6. Mad Grendel

    Mad Grendel

    Joined:
    Jun 12, 2014
    Posts:
    13
    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.
     
  7. Deleted User

    Deleted User

    Guest

    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.
     
  8. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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. ^_^
     
  9. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    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? day_night_tamaroq2s.jpg
     
  10. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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.
     
  11. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    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! sun_blocker_plane.jpg
     

    Attached Files:

  12. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
  13. Gooseman_1977

    Gooseman_1977

    Joined:
    Aug 15, 2013
    Posts:
    89
    wow, this looks awesome.. Has anyone tested it on mobile devices? How's the performance ?
     
  14. XMortX

    XMortX

    Joined:
    Nov 22, 2014
    Posts:
    1
    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.
     
  15. Asrock12

    Asrock12

    Joined:
    Mar 25, 2014
    Posts:
    1
    I have no words to thank you :D Thanks very much. Good job :);)
     
  16. m1a2x

    m1a2x

    Joined:
    Jan 5, 2015
    Posts:
    3
    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.
     
  17. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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.
     
  18. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    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.
     
  19. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    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.
     
  20. FureverDev

    FureverDev

    Joined:
    Jan 6, 2015
    Posts:
    2
    Looks good!
     
  21. Bramadian

    Bramadian

    Joined:
    Mar 22, 2014
    Posts:
    1
    How do i make it go slower, a day is now 5 seconds and night to.
     
  22. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    look 2 posts above you
     
  23. aditianekar

    aditianekar

    Joined:
    Mar 9, 2015
    Posts:
    1
    Its too good!! :):)
     
  24. i_am_mute

    i_am_mute

    Joined:
    Mar 29, 2015
    Posts:
    1
    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?
     
  25. achmedpwnd69

    achmedpwnd69

    Joined:
    Aug 5, 2015
    Posts:
    1
    Uh, I need to know how to make the clouds and stuff move and how to make the skybox dark at night. Help?
     
  26. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    I need help with that too, my day last forever,. but there is no night...
     
  27. jalajshah

    jalajshah

    Joined:
    Mar 5, 2018
    Posts:
    61
    re upload please