UE4动态生产Actor

该博客详细介绍了如何在3D游戏中创建一个浮动的物体,并实现子弹碰撞检测。通过使用浮动物体类AFloatingActor,结合定时器和碰撞回调函数,当子弹命中浮动物体时会触发粒子效果并销毁两者。同时,还展示了如何通过ASpawnActor类定时生成新的浮动物体,增加游戏交互性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是使用上次创建的浮动平台

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

#pragma once

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

UCLASS()
class AFloatingActor : public AActor {
	GENERATED_BODY()

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

	UPROPERTY(VisibleAnywhere, Category = "AAA")
	UStaticMeshComponent* VisualMesh;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	//子弹爆炸的粒子效果
	UPROPERTY(EditDefaultsOnly, Category = "AAA")
	class UParticleSystem* projectileParticle;
public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	//线程函数
	UFUNCTION()
	void checkOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloatingActor.h"
#include "Components/SceneComponent.h"
#include "TrueFPSProjectProjectile.h"
#include "Particles/ParticleSystem.h"
#include "Kismet/GameplayStatics.h"

// 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;

	//if (USceneComponent * ExistingRootComponent = GetRootComponent()) {
	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	VisualMesh->SetupAttachment(RootComponent);
}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay() {
	Super::BeginPlay();
	VisualMesh->OnComponentBeginOverlap.AddDynamic(this, &AFloatingActor::checkOverlap);
}

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

	FVector NewLocation = GetActorLocation();
	FRotator NewRotation = GetActorRotation();

	//获取物体当前的运行时间
	float RunningTime = GetGameTimeSinceCreation();
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
	float DeltaRotation = DeltaTime * 20.0f;    //Rotate by 20 degrees per second
	NewRotation.Yaw += DeltaRotation;
	SetActorLocationAndRotation(NewLocation, NewRotation);
}

void AFloatingActor::checkOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
	ATrueFPSProjectProjectile* projectile = Cast<ATrueFPSProjectProjectile>(OtherActor);
	if (projectile){
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), projectileParticle, GetTransform());

		projectile->Destroy();
		Destroy();
	}
}

新建一个包含AFloatingActor的Actor,命名为:SpwanActor

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

#pragma once

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

UCLASS()
class TRUEFPSPROJECT_API ASpawnActor : public AActor {
	GENERATED_BODY()

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

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

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

public:
	UPROPERTY(EditAnywhere, Category = "AAA")
	TSubclassOf<class AFloatingActor> subFloatingActor;

	float baseTime = 0.0;
};

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


#include "SpawnActor.h"
#include "FloatingActor.h"

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

}

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

	//每隔3秒生产一个Actor
	if ((baseTime += DeltaTime) <= 3.0){
		return;
	}
	baseTime = 0.0;
	UWorld* world = GetWorld();
	if (world){
		FVector spawnLocation = GetActorLocation();
		FRotator spwanRotation = GetActorRotation();

		FActorSpawnParameters spawnParam;
		spawnParam.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
		world->SpawnActor<AFloatingActor>(subFloatingActor, spawnLocation, spwanRotation, spawnParam);
	}
}

在这里插入图片描述
aaa

### 关于 Unreal Engine 4 中合成数据集的方法 在 Unreal Engine 4 (UE4) 中,创建和处理合成数据集通常涉及以下几个方面: #### 使用蓝图或Python脚本自动化场景生成 可以通过蓝图系统或者 Python 脚本来实现自动化的场景设置与渲染。例如,在蓝图中定义随机化参数(如物体位置、旋转角度、光照条件等),从而批量生成不同的场景配置并导出图像数据[^1]。 对于更复杂的任务需求,则可能需要用到外部工具配合完成整个流程;比如利用虚幻提供的插件接口编写自定义逻辑来控制摄像机运动轨迹以及捕捉特定视角下的画面作为训练样本的一部分[^3]。 #### 材质编辑器中的节点组合应用 材质编辑器的强大之处在于其灵活的节点网络结构允许开发者轻松构建复杂效果的同时也便于调整各层之间的关系以适应不同类型的视觉表现要求 。正如提到过的那样,“把材质图层放在我们基于节点的编辑器之中”,这使得艺术家们能够在不改变原有工作流的前提下引入新的特性——即通过编程方式管理和操作这些层次分明的信息单元。 此外值得注意的是虽然上述内容专注于描述如何高效地组织内部资源文件夹结构以便更好地管理大型项目内的资产关联性等问题 ,但它同时也间接反映了当涉及到具体实施细节时所应该考虑的一些最佳实践建议 :保持清晰有序命名习惯有助于后期维护期间快速定位目标对象所在位置及其用途说明文档记录完整准确无误等等都是不可或缺的重要环节之一 。 #### 借助第三方库扩展功能 如果标准版本的功能不足以满足特殊的数据采集需求,还可以探索社区贡献的各种开源解决方案或是商业授权产品。例如某些专门针对机器学习领域优化设计而成的中间件服务就具备强大的API接入能力,能够让使用者更加便捷地获取高质量标注好的图片序列用于后续算法开发测试阶段的工作当中去[^2]。 ```python import unreal_engine as ue def capture_scene(): # 设置相机参数 camera_actor = ue.get_editor_world().spawn_actor_from_class(ue.find_class('CameraActor'), ue.Vector(0, 0, 200), ue.Rotator(0, -90, 0)) # 配置分辨率和其他选项 settings = ue.create_struct('ScreenshotSettings') settings.resolution_x = 800 settings.resolution_y = 600 filename = 'C:/path/to/output/image.png' success = ue.take_high_res_screenshot(filename, settings) capture_scene() ``` 以上代码片段展示了怎样借助 Pyhton API 实现基本的画面捕获过程。当然实际应用场景下往往还需要进一步完善错误处理机制等方面的内容才能达到生产环境中稳定可靠运行的标准。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wb175208

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

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

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

打赏作者

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

抵扣说明:

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

余额充值