A first-person shooter (like Robot Dynamite) needs a first-person character. In contrast to a full third-person humanoid character, all the player can see is the gun and the first-person arms mesh. However, if it is a multiplayer game, or even a single player game in which one can look in a mirror or a closed-circuit video, the full third-person mesh is visible.
In Part I, we created an Unreal Engine 4 project and added some assets needed to build a first-person character. In Part II, we added a third-person humanoid mesh and made it controllable in the game and gave it a behind-view camera.
Now we shall add the first-person arms mesh and give the camera a first-person view.
This post assumes you have already completed Setting Up a First-Person Character in Unreal Engine 4: Part I and Setting Up a First-Person Character in Unreal Engine 4: Part II.
Add the First-Person Arms Mesh
- Re-open your Unreal Engine 4 editor project, “FirstPersonCharacter”. Load the blank level we created called “Overview”. Open the BP_Character blueprint and go to the blueprint editor window.
- Find the Components panel (by default, top left) and select the Camera component. Then click the green Add Component button. Start typing “Skeletal Mesh”, and when you see the Skeletal Mesh component in the menu, select it. Rename it “Arms” since that is what we shall use it for.
- With the Arms component still selected, find the Mesh category in the Details panel. Change the Skeletal Mesh property to SK_Mannequin_Arms. In the viewport, you will see the arms oriented the wrong way and above the camera. We shall add an animation and then fix that.
- With the Arms component still selected, find the Animation section in the Details panel.
- Set Animation Mode to Use Animation Blueprint, and set the Anim Class to FirstPerson_AnimBP.
- With the Arms component still selected, find the Transform category in the Details panel. Change the Location property to X=-2.0, Y=-4.4, Z=-160.
- Change the Rotation property to X=5.3, Y=1.9, Z=-19.9. The arms should look better on the camera in the Viewport.
- Play-test the game. You see both the mannequin and the arms (or at least, one hand of the arms). We need to set this character to a First-Person view.
Switching Between First- and Third-Person View
- Open the Project Settings window and select the Input section under Engine in the left sidebar menu.
- Find the Action Mappings property and press the Plus (+) sign to add a new input. Change its name to Toggle Third Person.
- Click the right-pointing triangle to open up the key binding dropdown. In the drop down, start typing “F7” and select the F7 menu item in the Keyboard category. Thus, the F7 key will be bound to the Toggle Third Person action. We still need to write code that says what this action actually does.
- Open the BP_Character blueprint and go to the blueprint editor window. Select the Event Graph tab.
- Add the following nodes to the event graph, setting the name of the timeline node as shown:
- Double-click the timeline node to open its editor.
- Set the length to 1.0 seconds. Make sure Use Last Keyframe is checked, and Autoplay and Loop are not checked.
- Click the “f+” button to add a new float curve. Name it Target Arm Length.
- Right click on the curve (it currently is just a horizontal line) and select “Add Key to CurveFloat_0”. Time and Value fields will appear.
- Change Time to 0 and Value to 0.
- Right-click again on the curve and select “Add Key to CurveFloat_0”. New Time and Value fields will appear. Change Time to 1 and Value to 600.
- Return to the Event Graph tab and edit the nodes you just placed to look like this:Play testing shows we have a basic switch from first to third person view, but a few things are broken: we still see both the arms and the mannequin at all times.
- Select the Arms component in the Components panel. In the Details panel, find the Rendering section and click the downward-pointing triangle to open up “Advanced View”. Check the box for Only Owner See, so that other players (and reflections and closed-circuit TV views) don’t see the Arms mesh.
- Modify the Event Graph nodes to look like this:Compile and save. Play testing looks much better now. However, there is still an issue: it starts in third-person view, but this code assumes it starts in first-person view. This is easy to fix.
- Select the Camera Boom component in the Components panel. In the Details panel, find the Camera section and change the Target Arm Length to 0.
- Then, select the Mesh (Inherited) component in the Components panel. In the Details panel, find the Rendering section and click the downward-pointing triangle to open up “Advanced View”. Check the box for Owner No See.
- Compile, save, and play test. Looks great!
There is more we can do to enhance this character, but that is a subject for another blog post (stay tuned!).
Till then, you can download a copy of the completed project here.