UE4笔记C++篇---Actor物体旋转

UE4笔记C++篇---Actor物体旋转_第1张图片


UE4笔记C++篇---Actor物体旋转

C++文件:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class TASK_API AMyActor : public AActor
{
	GENERATED_BODY()


	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Mesh", meta = (AllowPrivateAccess = true))
	UStaticMeshComponent* HealthMesh;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Mesh", meta = (AllowPrivateAccess = true))
	USceneComponent* Root_C;

public:	
	// Sets default values for this actor's properties
	AMyActor();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	void TurnAndTurn();
	
	FTimerHandle MyHandle;
	bool StopLooping;
};

// Fill out your copyright notice in the Description page of Project Settings.

#include "Task.h"
#include "MyActor.h"
#include "UnrealMathUtility.h"


// Sets default values
AMyActor::AMyActor()
{
 	// 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;
	StopLooping = true;
	HealthMesh = CreateDefaultSubobject(TEXT("MyStaticMesh"));
	Root_C = CreateDefaultSubobject(TEXT("HelloWorld"));
	RootComponent = Root_C;
	HealthMesh->AttachToComponent(Root_C, FAttachmentTransformRules::KeepRelativeTransform);
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	TurnAndTurn();
}

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

}

void AMyActor::TurnAndTurn()
{
	if (MyHandle.IsValid())
	{
		GetWorldTimerManager().ClearTimer(MyHandle);
	}
	if (StopLooping)
	{
		// 获得基础旋转
		FRotator MyRotator = HealthMesh->GetComponentRotation();
		// 弧度
		float Down = FMath::RandRange(0.1f, 0.05f);// 120°/360°
		// 角度
		float N = 57.4;// 180°/3.14

		MyRotator.Yaw += Down*N; // 角度

		HealthMesh->SetWorldRotation(MyRotator);
	}
	GetWorldTimerManager().SetTimer(MyHandle, this, &AMyActor::TurnAndTurn, 0.03f, true);

}



你可能感兴趣的:(UE4学习笔记,UE4笔记C++篇)