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

English original Address

In this simple tutorial, we will change the player’s view goal by simply smoothing and mixing motion at the start of the game.

Create a new C++ Actor subclass and name it SetViewTargetBlend. In the header file, we’ll declare an actor variable, call it MyActor, and make the new role editable anywhere.

 

SetViewTargetBlend.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ASetViewTargetBlend : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ASetViewTargetBlend(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* MyActor;
	
};
Copy the code

First, to have players, we need the #include Kismet/ gameplaystatics.h file.

#include "SetViewTarget.h"
// include gameplay statics header file
#include "Kismet/GameplayStatics.h"
Copy the code

In this case, all of our logic is in the BeginPlay function. We need by performing UGameplayStatics: : GetPlayerController (this, 0) to have the players. This will get the first player in the game scene.

Next, we’ll use SetViewTargetWithBlend(MyActor, 2.f) to set the view target of the player we have to the MyActor variable. We set the blending time to 2 seconds, which will indicate how long it takes the camera to move to the new target.

Here is the final.cpp file.

SetViewTargetBlend.cpp

#include "SetViewTargetBlend.h"
// include gameplay statics header file
#include "Kismet/GameplayStatics.h"


// Sets default values
ASetViewTargetBlend::ASetViewTargetBlend()
{
 	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ASetViewTargetBlend::BeginPlay(a)
{
	Super::BeginPlay(a);//Find the actor that handles control for the local player.
	APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this.0);
	
	//Smoothly transition to our actor on begin play.
	OurPlayerController->SetViewTargetWithBlend(MyActor, 2.f);
	
}

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

}
Copy the code

Compile the code. Drag and drop the new character into the game. In the editor, add a static grid to the MyActors variable in the Actor details panel.

Press the play button and the player’s camera moves to the new character within 2 seconds.

Results the following