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

English original Address

In the next tutorial, we will rotate an actor on each frame. Create a new C++ Actor class and name it RotatingActor. In the header file, we will create three floating point variables. We’ll set their UPROPERTY to EditAnywhere so we can change the value in the editor, and we’ll put all the variables in the Movement category to keep them together and separate from the other properties.

Here is the final header file.

RotatingActor.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ARotatingActor : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ARotatingActor(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 our float variables 	
	UPROPERTY(EditAnywhere, Category = Movement)
	float PitchValue;

	UPROPERTY(EditAnywhere, Category = Movement)
	float YawValue;

	UPROPERTY(EditAnywhere, Category = Movement)
	float RollValue;
	
};
Copy the code

Set all floating point variables to 0 by default in the.cpp file.

// Sets default values
ARotatingActor::ARotatingActor()
{
 	// 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;

	PitchValue = 0.f;
	YawValue = 0.f;
	RollValue = 0.f;

}
Copy the code

Create a FRotator variable called NewRotation in the Tick function and set it to the floating point variable created earlier. Next, create an FQuat variable (quaternion) and set it to NewRotatoin. Then, all we need to do is run the AddActorLocalRotation to get the actor to rotate into position.

Here is the final.cpP code.

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

	// on every frame change rotationg for a smooth rotating actor
	FRotator NewRotation = FRotator(PitchValue, YawValue, RollValue);
	
	FQuat QuatRotation = FQuat(NewRotation);
	
	AddActorLocalRotation(QuatRotation, false.0, ETeleportType::None);


	// The below method causes a bug for the pitch value. The pitch value stops updating at 90 degrees
	// this is a known bug in the Unreal Engine. 
	// solution found by ue4 user nutellis https://answers.unrealengine.com/questions/591752/pitch-rotation-stucks-at-90-90-c.html

	// FRotator NewRotation = GetActorRotation();

	// NewRotation.Pitch += PitchValue;
	// NewRotation.Yaw += YawValue;
	// NewRotation.Roll += RollValue;
	
	// OR add values using the FRotator Add function
	// FRotator NewRotation = GetActorRotation().Add(PitchValue, YawValue, RollValue);


	// SetActorRotation(NewRotation);

}
Copy the code

rendering

Once you add a Cube component to this actor and set the pitch, it rotates around the Y-axis (UE4 is the left hand coordinate system)