C++ object rotation float (similar to pickable object effect)

Create the c++ class Floating Actor

Generate Floating actor.h and Floating actor.cpp files

2.Floating Actor.h

Think of it as something like a directory for C++ classes. To begin compiling any new functionality, you must first declare all new variables or functions used in this file.

Add the following code under the statement AFloatingActor() :

	UPROPERTY(VisibleAnywhere) // Use the UPROPERTY macro command to make it visible in the Unreal editor
	UStaticMeshComponent* VisualMesh;// Declare a static grid component

	// Generate a field visible in the editor
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FloatingActor")
	float FloatSpeed = 20.0 f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FloatingActor")
	float RotationSpeed = 20.0 f;
Copy the code

The UPROPERTY keyword, used to specify how a property interacts with many aspects of the engine and editor.

Such as:

  • parameter role
    EditAnywhere Note This property can be edited on prototypes and instances through the properties window.
    VisibleAnywhere Note This property is visible in all property Windows, but cannot be edited.
    BlueprintReadWrite This property can be read or written from a blueprint.

3.Floating Actor.cpp

In AFloatingActor: : AFloatingActor () to add the code:

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

	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	VisualMesh->SetupAttachment(RootComponent);

	// Find assets
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

	// If the asset is found, set the mesh body
	if (CubeVisualAsset.Succeeded())
	{
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
		VisualMesh->SetRelativeLocation(FVector(0.0 f.0.0 f.0.0 f)); }}Copy the code

The path to find the asset can be directly dragged over the Model of Unreal Editor to directly generate the path.

AFloatingActor::Tick(float DeltaTime) add the code

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

	// Create a new vector and store the current location of the actor
	FVector NewLocation = GetActorLocation(a);// Create a new vector and store the actor's current rotation
	FRotator NewRotation = GetActorRotation(a);// The number of seconds (in game time) since this Actor was created, as opposed to the time in seconds to get the game.
	float RunningTime = GetGameTimeSinceCreation(a);//FMath::Sin Evaluates the sine of a scalar value to limit the value to [-1,1]
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	// Print DeltaHeight to the screen to see that the value is smaller
	UKismetSystemLibrary::PrintString(this, UKismetStringLibrary::Conv_FloatToString(DeltaHeight));
	NewLocation.Z += DeltaHeight * FloatSpeed;          // Adjust the height by FloatSpeed
	float DeltaRotation = DeltaTime * RotationSpeed;    // Rotation per second is equal to the Angle of RotationSpeed

	NewRotation.Yaw += DeltaRotation;
	// Set the actor rotation and position
	SetActorLocationAndRotation(NewLocation, NewRotation);
}
Copy the code

Using UKismetSystemLibrary: : PrintString need to add

#include “Kismet/KismetSystemLibrary.h”

Using UKismetStringLibrary: : Conv_FloatToString need to add

#include “Kismet/KismetStringLibrary.h”