Creating a Day/Night cycle

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.

Getting ready

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.

How to do it…

Perform the following steps to create a day/night cycle:

  1. As we discussed before, we can use Directional Lights to act like the sun. For our day/night cycle we will want our lighting to change over time, so let's select the 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.

    Note

    You can either rename the object from the Details tab or press F2 when selected in the Scene Outliner tab.

    How to do it…

    Making our DirectionalLight moveable

  2. To have this lighting change reflect correctly, the next thing we should do is build the lighting of our level by going to the toolbar above our level view and navigating to Build | Build Lighting Only.

    Now, if we were to rotate the light right now, we would see the Sky's color will be modified:

    How to do it…

    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.

  3. Next, from the top-center toolbar, navigate to Blueprints | Open Level Blueprint, and you should be brought to the following screen:
    How to do it…
  4. From the Blueprints editor, we right-click inside EventGraph and then select Add Event | Event Tick.
    How to do it…

    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.

  5. We want to have the Sky Sphere's sun at the same position as our Directional Light, so let's do that first. Go to the Scene Outliner tab and select the 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:
    How to do it…

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.

Note

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.

How to do it…

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!

How to do 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.

  1. Next, we want to have the directional light continuously rotate along the Y axis. To do this, let's first move the Update Sun Direction action and the SkySphere Blueprint object off to the side so that we can put something in between by selecting both items and then dragging them. Finally, highlight the arrow from the left side of the Update Sun Direction action, hold down the Alt key and then click in order to break the connector.
    How to do it…

    The current state of the Event Graph (note the broken connection)

  2. From there, select the 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.
    How to do it…

    Working with DirectionalLightMoveable actor in the blueprint

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:

  1. To the left of the Delta Rotation vector, right-click and create a Make Rot action (it makes a rotator from a Pitch, Yaw, and Roll value). Connect the Return Value from the Make Rot action to the Delta Rotation vector of the Add Actor Local Rotation action.
  2. To the left of the Make Rot action, right-click and create a Float * Float action, which we will use to multiply two floating point numbers together (in coding, the operator for multiplication is *). 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.
    How to do it…

    Using delta seconds within our rotation

  3. At this point, click on the Compile button, exit the level blueprint, and run the game!
    How to do it…

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!

See also

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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.100.20