If we are creating an open world title or any kind of game where the player is outdoors, one thing that would be nice is the ability to have our world change the time of day at the same rate.
Before we start working, we should have a level created with a Sky_Sphere
and a directional light (the default map has this). To be sure, you can use the DayNightCycle-Before
map from the example code folder.
Perform the following steps to create a day/night cycle:
DirectionalLightStationary
object from the Scene Outliner tab and then from the Details tab, change the Mobility property to Movable
. Finally, change the name of the object to DirectionalLightMovable
.Now, if we were to rotate the light right now, we would see the Sky's color will be modified:
However, the sun that is part of our SunSphere
will not be modified by the changes. To fix this, we will need to use Unreal's Blueprints system.
This creates a new event that will get called every frame that it is active and it has a parameter of Delta Seconds, which is how much time has elapsed for this particular frame.
SkySphereBlueprint
object by clicking on it. Once this is done, go back into the Blueprints screen and to the right of the Event Tick event, type in Sun
to show the properties that contain those letters. From there, select the Update Sun Direction option:If the object was selected correctly, we should see the Target variable already filled in with the SkySphere
.
To zoom in, use the mouse wheel. This action will find the rotation of the directional light in our level and modify it so that the sun will face the same way.
If you want to see what Update Sun Direction is actually doing, feel free to double-click on the action. We'll talk about what those actions mean later on in Chapter 8, Blueprint Scripting - Level Effects.
Blueprints are run while the game is played, so at this point, if we play the game, selecting the DirectionalLightMovable
object, and rotating it from the Details tab, we'll notice that the sun in the sky rotates to face it!
Of course, at this point, we could rotate the light using a matinee for cutscenes and we'd be done, but in this instance, we want it to move automatically, so let's modify the level blueprint to do that.
DirectionalLightMoveable
actor from the Scene Outliner tab and then right-click on Event Graph and select Add Actor Local Rotation (searching for Rotation
from the top search bar). Connect the Event Tick event's output arrow to the Add Actor Local Rotation node and then connect its output to the Update Sun Direction input. Finally, change the P (Pitch) value in the Delta Rotation property of the Add Actor Local Rotation action to 10
.The Add Actor Local Rotation node takes whatever value the Target object (directional light) has as its current Rotation and adds to it by the Delta Rotation vector. We do this before we update the sun's direction because we want the sun and the light to be at the same position (the sun would be a tad off, otherwise). The 10
value acts as the speed at which we want to move in that direction and can be modified to change the speed of our movement.
Now if we play the game, we'll notice that the sun is moving exactly as we wanted, but it is moving extremely fast! We could modify this value to fit whatever speed we'd like, but first, there's something that we need to note as we build even more complex actions—the Delta Seconds property that we talked about earlier.
The Delta Seconds value (commonly referred to as the Delta Time or dt in mathematics) is extremely important because in games, even though we often want things to run as quickly as possible (usually 60 times per second), the more complex or graphically intensive things get, the more time it takes the computer to do all of the calculations needed to update and render the completed frame. Using this value as a modifier makes sure that our events are dependent on time, not by frame, ensuring that the same things happen as time goes on. Since the value will also normally be 1/60 (or ~0.016), being multiplied by 10 will give us a much smaller number, which is what we want.
This value is a float, or floating point number; this is a variable or container of information that we can store and use in various areas of code.
Taking that into account, let's use the Delta Seconds value in creating our Delta Rotation:
*
). Connect the Delta Seconds value from Event Tick into one of the two inputs and connect the output to Yaw of the Make Rot action. Finally, put in a value of 10
or whatever modifier you want into the other input.It works perfectly and we can modify this 10 value to make the value go faster (20 for 2x faster) or slower (1 for 10x slower), without having to worry about how complex the game is!
We just touched the tip of the iceberg when it comes to working in lighting! After all, we can't condense what someone's full time job is to 20 pages. However, I do have some external resources that may be useful should you decide to do more with it:
18.117.100.20