[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English original Address

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

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

SetViewTarget.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ASetViewTarget : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ASetViewTarget(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 SetViewTarget(MyActor) to set the view target of the player we own to our MyActor variable.

Here is the final.cpp file.

SetViewTarget.cpp

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


// Sets default values
ASetViewTarget::ASetViewTarget()
{
 	// 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 ASetViewTarget::BeginPlay(a)
{
	Super::BeginPlay(a);//Find the actor that handles control for the local player.
	APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this.0);

	//Cut instantly to our actor on begin play.
	OurPlayerController->SetViewTarget(MyActor);
	
}

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

}
Copy the code

Compile the code. Drag and drop the new actor 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 will be displayed on the new actor.

The renderings are as follows

The first player’s perspective

 

The Actor’s point of view