If I remember correctly I was supposed to spend my time making more plants and foliage this week. Couldn’t keep that promise! I came across a video of a day cycle and I had to implement it to my world. I’m lucky I did, because now my world is much more pleasant to work with.

This is a long post so for all those “Too long, didn’t read” people out there, below is a video of the day cycle. I’ll explain the details later in this post.

Note: The original day cycle was made to last one hour in real time, but I scaled it down to ~30 seconds. For that reason it has some timing issues with the lights and fog.

How the day cycle works

The basic idea is that a blueprint rotates the sun around the landscape on Y-axis and keeps updating the skysphere, to show correct colors. This rotation has two threshold values that set a boolean “day”. This boolean is used to create day and night events, which can be used for external functions that need to be launched when night or day begins. In my case an exponential height fog rises from the ground when night begins and goes away when sun is rising.

I also made an in game clock that I plan to show for the player in the HUD. The time is printed on the screen for debugging purposes, at the moment.

Below is the whole blueprint and as you can see, it’s not so complicated system. The settings that I have in these images are still set for the demo, and they are not in the actual scale. I don’t have enough time to set everything to correct scale, because I want this post out today.

I will break down the code from left to right. I recommend checking this image every now and then, while you continue reading, to see where the piece of code is located in the big picture.

1. Creating Rotation And Clock

Float “Game Start Hour” is used to control the time of day where my scene is when I press play.

Float Curves Inside The “Day Rotation” Timeline

Top one is the actual rotation of the sun on Y-axis and lower one translates the length of timeline in real time, into 24 hours in game time.

“Time Of Day” float from the timeline node is fed to a custom function “Game Time”. I made this function to turn time’s decimal values into hours, minutes and seconds (Details on this function below). When the numbers are modified by the function, they are fed to a format text node so that I can print something readable on the screen.

I’m also setting Integer “Game Hours” here, because I though I might need it later.

Game Time Function

Game Time function that turns decimal values into hours, minutes and seconds. I’m sure there might be easier way to do it, but this is the first solution that came to my mind and it works 😀

For example the time of day decimal value is 13.5055

Remove decimals and you have hours. Save it.

13,5055 – 13 = 0,5055

0,5050 * 60 = 30,33

Remove decimals and you have minutes. Save it.

30,33 – 30 = 0,33

0,33 * 100 = 33

That’s seconds. Save it

Now I have:

Hours: 13

Minutes: 30

Seconds: 33

Or in other words, in game time is: 13:30:33

Updating Sun Rotation And Sun Location In The SkySphere

These two nodes are responsible of moving the sun in the sky and changing the sky’s color.

Sun rotation float from the timeline is fed to a “make rotation” node and that rotation comes to the setActorRotation node in the image below.

2. Debug Printing

Printing Numbers On The Screen To Help Adjusting Values

At this point of the code I’m printing sun’s current rotation and sun’s and skylight’s intensity. This helps me to see the values during game play. If the numbers aren’t what I want them to be, I can pause, tweak them and continue playing to see difference.

3. Setting “Day” And Creating Day And Night Events

So here’s the trick. The paint masterpiece below shows how the angles in Y-axis are inside the game. Red line is my landscape and -90 degrees is up.

Now that I know which direction is which angle, I can say “if sun’s current rotation is more than -180 and less than zero degrees, boolean day is true”. So now my blueprint knows if it’s day or night and I can start doing some changes in the world, by using this information.

I didn’t come up with the “Checking if it’s Day” part. I found it from an Unreal Engine 4 tutorial. It’s purpose is to only launch the “day” and “night” events when they start and to block them from launching every frame.

If day is true, launch event “Becomeday”. if day is false, launch event “BecomeNight”.

4. Making Changes To The World

Now I have another Timeline named “Intensity Curves”, which contains curves to change sunlight and skylight intensities. When night starts, the intensities are driven down. When day starts again, these changes are inverted.

That was the basic system to rotate the sun, update the sky, create a clock and control the light intensities. Now I will explain my fog system that is driven by “BecomeDay” and “BecomeNight” events.

Here’s the whole function. Red nodes are the day and night event’s.

This is how it works: when night starts, make fog visible and start playing timeline. This timeline has a float curve going from -4000 to 500 during time X.

The float from timeline is made into a vector and the vector is fed to the fog’s SetWorldLocation node. That makes the fog move up through the ground and stop when it’s above ground.

What should I do when day starts? If I would use the exact same function, the fog would suddenly disappear while it’s still visible. So instead I start by playing the timeline in reverse and when the fog’s location in Z-axis is less or equal to -3500 the branches condition becomes true and fog’s visibility is set to false

This blueprint has many adjustable aspects that can change the looks of the game. Since I now have the day and night events, I can use them or the clock to launch any event I want in the game, e.g. spawn different monsters at day and night and make the local Inn open it’s doors at six in the morning and close in the middle of the night.

You’re free to copy this blueprint or parts of it, if you find it useful. To make the day last one hour in real life, set the length of “day rotation” timeline to 3600 seconds and set “Game Start Hour” floats multiplier to 150.

That’s all folks!

Thanks For Reading And Good Night!