Welcome to the tutorial on setting the real world sun position in Unreal Engine 5.
Today, we’ll look at how to build a system that precisely matches the real-world sun position within your Unreal Engine project.
This means that the sun you see on the screen will match the actual location of the sun at a specific time of day and year. You will achieve precise control over the sun’s geographic location based on latitude, longitude, date, and time.
It’s an excellent tool for making your sun appear more realistic, especially if you want to represent a certain city or nation or if you work in architectural visualization.
Unreal Engine 5 makes it surprisingly easy to set the real-world sun position. Let’s get started!
Plugin: Sun Position Calculator
First, we must enable a built-in plugin named “Sun Position Calculator” by Epic Games, Inc.
To activate it, go to the ‘Edit‘ menu in the top left corner, then select ‘Plugins.
Search for “‘Sun position calculator” in the plugins window and enable it. It is completely free and does not require any further downloads.
After activating the plugin, restart your engine to apply the modifications.
Setting the Real World Sun Position in Unreal Engine
Once you’re back in the engine, open the Content Browser (Window -> Content Browser), and search for the “SunSky” Blueprint Class.
It’s located in the “Engine -> Plugins -> Sun Position Calculator Content” folder.
Simply drag and drop the Sun Sky Blueprint into your level to get started.
If you can’t find it after installing the plugin, open the Settings (right corner) in the content browser and enable both Show Engine Content and Show Plugin Content.
Common Lightning Errors
Duplicated Lights: To start using the SunSky blueprint, it’s important to clean up your scene by removing any pre-existing Directional Light, Sky Atmosphere and Sky Light. These lights are unnecessary since the SunSky blueprint already includes them.
White Screen: If your screen becomes white after dragging the SunSky blueprint, open the blueprint by double clicking on it. Once it’s opened select the Directional Light on the Components panel. Then, in the Details panel, look for the “Intensity“ parameter. Just change the light intensity value to 3 lux or another value that suits you.
Black Screen: If the time you choose is during the night, the screen will turn black.
Sun Positioning Based on Geographic Location
Now that we’ve got everything set up, we can start using the real-world sun position function. This tool allows you to fine-tune the Sun’s geographic location depending on latitude, longitude, date, and time.
Let us look for a city to use as an example!
I’ll set the current sun position in Barcelona. If you want the sun position of another city, I recommend the nations geo website. Or just google the city coordinates and UTC.
To continue, just select the SunSky in the Outliner, and enter the values in the Details panel.
|Time Zone||2 (Use only UTC or GMT)|
|Use Daylight Saving Time||I recommend avoiding the use of this option as it sometimes doesn’t work correctly. Instead, consider using Daylight Saving Time UTC, in this case UTC 1 instead of 2.|
- North Offset: The relationship between the items in your level and the compass’s cardinal points is controlled by this parameter. Avoid using the Rotate tool to rotate the compass itself in the Level Viewport. Only control the cardinal points while using this option.
- Month and Day: Simply enter the day of the year you wish to replicate.
- Solar Time: Here just use a time that works for you. When you hover over the time setting, you’ll see it’s shown as a floating-point value. For example, 12.5 = 12:30. This enables for exact control of the sun’s position throughout the day.
How to update the Sun Position in real time?
If you want to change the position of the sun in real time during the day, you will have to modify the Sun Sky blueprint code.
Feel free to modify the “Update Sun” function. Start by disconnecting the time variables and then connect the time values to the “UTC Now” Blueprint function. It is recommended to utilize this function instead of “Now” since the Time Zone will be added or subtracted from the time value.
If you are unsure about the difference between “UTC Now” and “Now”, simply connect the nodes to a Print String node and check the output.
Right-click on the “Return Value” output of the UTC Now node and select “Split Struct Pin” to connect the corresponding output pins to the “Get Sun Position” function. Repeat the same process in the Event Graph.
Alternatively, you can connect the return nodes directly to time nodes such as “Get Year“, “Get Month“, “Get Day“, “Get Hour“, and so on. Or you can just connect a “Break Date Time” node.
The sun position will update as soon as you start playing. You can verify this by adjusting the local time on your PC. If changing the time on your PC causes the sun’s position to shift, this confirms that the system is working successfully.
To improve performance, consider replacing the Event Tick in the Event Graph for an Event BeginPlay. Connect it to a Delay node that calls the “Update Sun” function every 60 seconds. This approach will prevent the constant checking of the sun position on every frame.
And that’s it! You have successfully set up the real-world sun position in your Unreal Engine 5 project.
Now, when you look up at the sky in your game, you’ll see the sun accurately positioned, matching the real-world location and time. Feel free to experiment with different locations and times to achieve the desired atmosphere.
I hope you found this tutorial helpful. Happy game development!