How to switch between first person and third person in UE4?

There are often first-person and third-person perspective switching scenes in the game. The author briefly introduces how to perform this step.

1. Create a character

Add 2 Character blueprints in the content browser, named FirstPersonalCharacter and ThirdPersonalCharacter

Double-click to open the FirstPersonalCharacter blueprint editor, add the Camera component in the component window, adjust it to the appropriate position, and check the Use Pawn Control Rotation option in CameraSettings.

Add movement logic to the event graph (not much to explain its principle and operating mechanism here)

Do the same in the ThirdPersonalCharacter Blueprint, but with a few extra steps. Add SpringArm in the component window, drag and drop Camera into its children, and then adjust the camera position.

Set the class to the 3rd person blueprint in the animation of the Mesh component

Set the skeleton of the Mesh to SK_Mannequin (this is a free character skeleton in the Unreal Marketplace, you can add it to the specified project after downloading), and then adjust the character's orientation and the character's position in the capsule body. If the reader has other character bones and animation, you can set it according to your personal preference

The next important step is to search for rotation in the mobile component, check Orient Rotation to Movement, search for rotation in the third-person self component, uncheck Use Controller Rotation Yaw, search for rotation in SpringArm component, check Use Pawn Control Rotation.

After these three steps are set, the character can turn normally.

2. Switch person logic

Open the level blueprint to create the Event BeginPlay event, get the third-person character at the start of the game and save it, and then cast (create) a first-person Character into the scene (if the default game character in the game mode is set to be first-person, create a first-person character A three-person character, but since the first person has no skeleton model and is transparent, it will not appear abnormal when placed in the level scene in advance), try to avoid collision in the position (although there is no skeleton model, but the collision of the capsule body still exists), and save the output.

Add C key-driven event, as the key to switch from third person to first person

First get the position of the third-person character and save it, and adjust the position of the first-person character after switching the perspective. At the same time, the judgment logic is added to avoid triggering the event in the first-person perspective (the initial value of Is3P is set to True, because the default is the third-person character perspective when the level is running).

Add the B key-driven event, as the key to switch back to the third person from the first person

Since the position of the third-person character already exists in the scene, there is no need to adjust it. Instead, change the position of the first-person character back to the state before the switch to ensure that a transparent collision block does not appear in the scene. Judgment logic is also added to avoid repeated triggering of events.

Running the level, the functions of the C key and the B key are running correctly, and the switching function is normal.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326003624&siteId=291194637