Now that we have some experience working with Blueprints, let's modify one that's already been created for us. In this section, we will cover how to add a flashlight to our game's character.
Before we start working within the Unreal Editor, we will need to have a project to work with. Follow these steps:
Cookbook_Chapter8
). Once you are done, click on Create Project.Example Code
folder and drag and drop the NightScene
map provided in your project folder and open it:At this point, if you were to play the game, it would be incredibly dark, as shown in the following screenshot:
For those who have gone through Chapter 6, Lighting and Shadows, this should look quite familiar. In that chapter, we actually created a flashlight and then attached it to our player at runtime.
However, we can modify the character's blueprint to make it so that our player will always have a flashlight on. Follow these steps:
Blueprints
folder. Find the MyCharacter
file.This blueprint is what's spawned when the game is started. It contains all of the logic for movement, the camera, and the shooting behaviour.
This section is similar to our level in that it has objects and they're placed in the world of our object, but it's apart from the level. You can move around the viewport on the right-hand side in exactly the same way as the main game. Zoom out so that you can see everything.
You can see that the spotlight is already added for us.
You'll notice a couple of issues. First of all, the light is lighting the player in addition to what's in front of it. Also, if we look up and down, it does not move with us.
MyCharacter
blueprint and select our SpotLight object and move it (if the Move tool isn't there, press W). Notice that when we move it in the blueprint, it modifies what is done in the game. Move the light until it is in front of the barrel of the gun.This solves the issue of lighting the player, but moving up and down is not working. To do this, we can make the SpotLight object a child of the camera. That way, whenever it moves, the light will move as well. Sadly, we cannot change this while the game is running.
SpotLight
object on top of the FirstPersonCamera
object. You should see a green checkmark, saying that we can attach it.SpotLight
objects and set the same parameters that we did in the Adding moveable lights – flashlight, part 1 recipe of Chapter 6, Lighting and Shadows or translate the object so that it is in front of the camera with Inner Cone Angle of 8
and Outer Cone Angle of 10.
keyboard events
. This will display all of the keyboard keys and allow us to trigger events based on when they are pressed or released.MyCharacter
blueprint, and Play the game!With this, we now have a flashlight that will follow our player's camera in all directions and exist in every single level we have in the game! Here, you learned how to modify the already existing blueprints and added in some simple player input using the F key to toggle visibility.
3.139.81.210