The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

Here is a tutorial for Epic’s game Control camera tutorial.

Create a new role called CameraDirector. Then add three variables to the header file. Add two camera actors inherited from the Actor class. Set the UPROPERTY to EditAnywhere so we can add actors to the editor. These two cameras will be the actors for our view switch. Add a float variable (representing time) so I can use it in the CPP file. Below is the header file.

CameraDirector.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"

UCLASS(a)class UNREALCPP_API ACameraDirector : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ACameraDirector(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// declare variables
	UPROPERTY(EditAnywhere)
	AActor* CameraOne;

	UPROPERTY(EditAnywhere)
	AActor* CameraTwo;

	float TimeToNextCameraChange;
	
};
Copy the code

First, we want to add the Kismet/ GamePlaystatics.h script to allow us access to the player controller.

#include "CameraDirector.h"
// include Kismet/GameplayStatics.h
#include "Kismet/GameplayStatics.h"
Copy the code

The rest of the logic for this actor will be added to the Tick function. Declare two floating point variables. These two floating variables manage the amount of time between camera changes and the amount of time between smooth mixed views.

void ACameraDirector::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	const float TimeBetweenCameraChanges = 2.0 f;
    const float SmoothBlendTime = 0.75 f;

}
Copy the code

Next, subtract DeltaTime from the float variable TimeToNextCameraChange that we declared in the header file.

// Called every frame
void ACameraDirector::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    
	const float TimeBetweenCameraChanges = 2.0 f;
    const float SmoothBlendTime = 0.75 f;
	TimeToNextCameraChange -= DeltaTime;

}
Copy the code

After that, we want to check whether the TimeToNextCameraChange is less than or equal to 0. If true, add TimeBetweenCameraChanges (set back to 2 seconds) to TimeToNextCameraChange. Get the PlayerController by using UGameplayStatics method GetPlayerController. By getting the PlayerController, we can set its view target. We check if the view target is CameraOne and switch the camera appropriately based on the result.

  • Get the Player controller
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this.0);
Copy the code

  • Set camera targets in a smooth mix
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
Copy the code

  • Set the camera target directly
OurPlayerController->SetViewTarget(CameraOne);
Copy the code

Below is the complete CPP file

// Called every frame
void ACameraDirector::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	const float TimeBetweenCameraChanges = 2.0 f;
    const float SmoothBlendTime = 0.75 f;
	TimeToNextCameraChange -= DeltaTime;
	
    if (TimeToNextCameraChange <= 0.0 f)
    {
        TimeToNextCameraChange += TimeBetweenCameraChanges;

        //Find the actor that handles control for the local player.
        APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this.0);
        if (OurPlayerController)
        {
            if (CameraTwo && (OurPlayerController->GetViewTarget() == CameraOne))
            {
                //Blend smoothly to camera two.
                OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
            }
            else if (CameraOne)
            {
                //Cut instantly to camera one.
                OurPlayerController->SetViewTarget(CameraOne); }}}}Copy the code

Drag and drop the CameraDirector Actor into the scene. Drag two cameras (or any actor) into the scene and set them to CameraOne and CameraTwo. Now press Play and you’ll see your view target move from one camera to the other.

\