【UE4】让Actor上下漂浮且旋转

效果展示

请添加图片描述

h

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

#pragma once

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

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

public:
	UPROPERTY(VisibleAnywhere);
	UStaticMeshComponent* VisualMesh;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Speed");
	float FloatSpeed = 20.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Speed");
	float RotationSpeed = 20.0f;

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

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

cpp

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


#include "MyActor.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;


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

	//添加立方体 一般不写死,可以手动去选择
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
	//如果构造成功
	if (CubeVisualAsset.Succeeded())
	{
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
		VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	}

}


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

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

	//获取Actor当前位置及旋转
	FVector NewLocation = GetActorLocation();
	FRotator NewRotation = GetActorRotation();

	//获取自创建以来的游戏时间
	float RunningTime = GetGameTimeSinceCreation();
	//计算高度增量
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	NewLocation.Z += DeltaHeight * FloatSpeed;
	float DeltaRotation = DeltaTime * RotationSpeed;
	NewRotation.Yaw += DeltaRotation;
	//将新的旋转Yaw以及高度设置给Actor
	SetActorLocationAndRotation(NewLocation, NewRotation);
	
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
UE4中,遍历Actor是非常常见的需求,比如我们要在场景中获取一个类型的Actor,并对它们进行某些操作,或者获得某些信息。UE4提供了多种遍历Actor的方法,具体如下: 1. 遍历所有Actor 通过获取场景中所有的Actor,我们可以遍历到每一个Actor,并且可以通过Actor的类型或者Tag等属性进行筛选。例如,我们可以通过以下方式获取场景中所有的Actor: ``` for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr) { AActor* Actor = *ActorItr; // ... } ``` 通过上述方法遍历所有的Actor,但是效率不高,因为包含场景中所有Actor,所以应该尽可能缩小作用范围。 2. 遍历场景中指定类型的Actor 在上文中,我们通过遍历所有的Actor来获取信息,但是这样效率较低。如果我们明确知道需要获取的Actor的类型,可以直接使用TActorIterator,指定类型,从而获取对应类型的Actor,例如: ``` for (TActorIterator<AMyActor> ActorItr(GetWorld()); ActorItr; ++ActorItr) { AMyActor* MyActor = *ActorItr; // ... } ``` 这样,我们只会得到指定类型的Actor,效率会更高。 3. 遍历同一类型的Actor 在某些情况下,我们可能需要遍历场景中指定类型的Actor,并且需要在其中特定的Actor中进行操作。这时候,我们可以使用UE4提供的UObjectIterators。这些迭代器一般用于UE4资源的遍历,通过`FindObject<UClass>`可以获取到一个指定类型的迭代器,例如: ``` for (TObjectIterator<AMyActor> It; It; ++It) { AMyActor* MyActor = *It; // ... } ``` 其中,TObjectIterator处理UObject类型,且不仅限于Actor,可以操作场景中的所有UObject。 4. 遍历指定组件类型的Actor 有时候我们可能只需要获取场景中特定Actor身上的特定组件,使用Components查询实例化Actor即可。 ``` for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr) { TArray<UMyComponent*> MyComponents; ActorItr->GetComponents<MyComponent>(MyComponents, true); for (UMyComponent* Component : MyComponents) { // ... } } ``` 利用GetComponents 指定组件类型,获取所有指定组件类型的数组。 以上就是UE4中常见的遍历Actor的方法,我们可以根据实际需求灵活应用。需要注意的是,遍历所有Actor可能会影响游戏的性能,因此应该尽可能明确地获取需要操作的Actor类型或者位置,并缩小作用范围。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非西昂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值