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

English original Address

Provides a tutorial section, in this tutorial, we will learn how to use the SetActorLocationAndRotation function. Create a new c + + Actor subclasses and name it SetActorLocationAndRotation. Create FVector and FQuat variables in the header file, respectively, and make them editable anywhere by setting the UPROPERTY to EditAnywhere. Also place these variables in the Location category to keep them together and separate from the other attributes.

Here is the final header file

SetActorLocationAndRotation.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ASetActorLocationAndRotation : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ASetActorLocationAndRotation(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;

	UPROPERTY(EditAnywhere, Category = Location)
	FVector NewLocation;

	UPROPERTY(EditAnywhere, Category = Location)
	FQuat NewRotation;
	
};
Copy the code

In this case, we will call SetActorLocationAndRotation function in BeginPlay function. To learn more about SetActorLocationAndRotation function information, please click here.

Here is the final.cpp file.

SetActorLocationAndRotation.cpp

#include "SetActorLocationAndRotation.h"


// Sets default values
ASetActorLocationAndRotation::ASetActorLocationAndRotation()
{
 	// 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 ASetActorLocationAndRotation::BeginPlay(a)
{
	Super::BeginPlay(a);SetActorLocationAndRotation(NewLocation, NewRotation, false.0, ETeleportType::None);	
	
}

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

}
Copy the code

Compile the code. Drag and drop the new character into the game. Add a static grid component to the actor. In the editor, set a value for NewLocation and NewRotation, and when you click the Play button, actor will position and rotate to these coordinates.

Effect diagram

Before the game runs

 

After the game runs