Unreal Engine 4 C ++ creates a Zoom camera for the role (source code)
In the game, observing the role's camera can narrow down and pull far is a common requirement, and UE4 is easy to implement. This article uses the TopDown template as an example to explain how to implement a Zoom camera step by step.
After creating the C ++ project of the TopDown template, we will get three important classes. AXXXCharacter class to Implement role control and behavior. AXXXGameMode class: the default GameMode of the Project, which defines the default role controller and default Pawn. AXXXPlayerController, role controller class, receives input control, and guides the role to perform operations. We mainly modify the AXXXCharacter class.
According to the principle of Zoom implementation, UE4 implements a USpringArmComponent class for camera collision, which can automatically handle camera-related collisions, indentation, and other functions. To achieve Zoom, you only need to properly adjust the value of the TargetArmLength variable. Very simple.
First, we define the Zoom configuration parameters. Add the following struct to the XXXCharacter. h file:
USTRUCT()struct FZoomData{GENERATED_USTRUCT_BODY()UPROPERTY(EditDefaultsOnly, Category = Config)float MinCameraLen;UPROPERTY(EditDefaultsOnly, Category = Config)float MaxCameraLen;UPROPERTY(EditDefaultsOnly, Category = Config)float ZoomStepLen;};
MinCameraLen, which defines the nearest camera distance; MaxCameraLen, which defines the maximum camera distance; ZoomStepLen, which defines the Zoom distance for each small step. Note the USRUCT () prefix. you can configure this struct in the editor.
Step 2: Add configuration parameters for the AXXXCharacter class.
UPROPERTY(EditDefaultsOnly, Category = Config)FZoomData ZoomConfig;
Step 3: add the ZoomIn and ZoomOut methods for AXXXCharacter. These two methods implement the Zoom function.
UFUNCTION(BlueprintCallable, Category= Camera)void ZoomIn();UFUNCTION(BlueprintCallable, Category = Camera)void ZoomOut();
Step 4: add the overload method SetupPlayerInputComponent for AXXXCharacter. This method initializes the input binding.
protected:virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
The specific implementation of each method is as follows:
void AsqxgameCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent){check(InputComponent);InputComponent->BindAction("ZoomIn", IE_Pressed, this, &AsqxgameCharacter::ZoomIn);InputComponent->BindAction("ZoomOut", IE_Pressed, this, &AsqxgameCharacter::ZoomOut);}
The SetupPlayerInputComponent method binds two actions named ZoomIn and ZoomOut, which are set in the editor. Choose Edit> Project Settings> Input Project.
I bound the two actions to the Mouse Wheel Up and Mouse Wheel Down buttons respectively.
void AsqxgameCharacter::ZoomIn(){if (CameraBoom->TargetArmLength >= ZoomConfig.MinCameraLen){CameraBoom->TargetArmLength -= ZoomConfig.ZoomStepLen;}else{CameraBoom->TargetArmLength = ZoomConfig.MinCameraLen;}}void AsqxgameCharacter::ZoomOut(){if (CameraBoom->TargetArmLength <= ZoomConfig.MaxCameraLen){CameraBoom->TargetArmLength += ZoomConfig.ZoomStepLen;}else{CameraBoom->TargetArmLength = ZoomConfig.MaxCameraLen;}}
The implementation of ZoomIn and ZoomOut methods is very simple.
Compile the Code, set the ZoomConfig value in the Character blueprint, and execute the game to see the result.
Project Source: http://git.oschina.net/cloudsource/UE4-Code