Customizing the player camera Blueprint so that it is a side scrolling camera
Now that we can see our Character, we can now also make the side scrolling camera. Double click on your PlayerCamera Blueprint. This will open up the Blueprint Editor.
In the left hand panel, you will see an entry called “KismetUpdateCamera”. This is an event that can be implemented in Blueprints. To make it available, first click on the plus symbol. Then double click on the entry to open the Blueprint implementation up. You will be presented with a new window with two nodes already connected. This is the default implementation and from here we can modify it to make the camera do something else.
Evaluating the camera location to achieve a side scrolling camera
The basic logic that we want for this, is to get the location of the pawn and then add an offset. This places the camera away from the pawn’s location which is the base of many types of cameras such as third person, bird’s eye view and so forth.
To start, we need to get the pawn’s location. Right click in the Blueprint work space and search for “Get Owning Player Controller”. Click on the menu option to create a Blueprint node. This node has a blue pin on the right which outputs a reference to the Player Controller that owns this Player Camera.
Left click and drag the pin out and release in the Blueprint workspace. This will automatically bring up a new menu which you can then use to create the “Get Controlled Pawn” node. This node has a blue pin on the right which outputs the pawn which is controlled by the Player Controller.
Left click and drag the pin out and release in the Blueprint workspace. This will bring up a new menu which you can use to create the “Get Actor Location” node. This node has a gold pin on the right which outputs a vector which represents the pawn’s location.
With three nodes, we were able to get the player’s pawn location. Easy! Next we want to add an offset to this.
Blueprints are able to create new variables which they can use and expose to other Blueprints via Interfaces (more on that later). We need to create two variables here to create the offset we want to apply to the pawn’s location.
Start by clicking on the Variables tab in the lower right hand panel of the Blueprint Editor. Create a float by using the drop down box and click on float. Then set a name such as “Camera Distance” and click “Add”. Next create a vector using the same method and give it a name such as “Camera Offset”.
From here, let’s set some sensible values to these new variables before continuing on. Click on the “Defaults”, this opens the familiar default properties panel. Find your new variables and set them to values such as:
- Camera Distance = 512
- Camera Offset = (X=0,Y=1,Z=0)
Switch back to the Blueprint workspace by click on the “Scripts” button. The logic that we can use to create the camera offset is to normalize the camera offset, multiply it with the camera distance and then add it to the pawn’s location.
To get the “Camera Offset” in the workspace, click and drag the variable onto the workspace. This will open a context menu with options to “Get” or “Set”. Select “Get”. This node has a gold pin on the right which outputs the value of the camera distance. Left click and drag from the golden pin to create a “Normalize” node. This will normalize our camera offset. Left click and drag from the Normalize’s golden pin to create a “FVector * float” node. In a similar fashion, create a node to get the “Camera Distance” and hook it up to the "FVector * float* node. Lastly left click and drag from the “FVector * float” golden pin and create a “+ (FVector)” node. Connect the “Get Actor Location” gold pin to the other gold pin of the “+ (FVector)” node.
With the final logic in place, you can now connect up the final result ["+ (FVector)"] to the “Return Node”'s “New Camera Location”.