【项目实训】UE4 C++ 物体生成
2021/7/18 12:35:55
本文主要是介绍【项目实训】UE4 C++ 物体生成,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
(第三周)文章三
生成函数
生成函数有很多重置这里只提一种
GetWorld()->SpawnActor<T>(UClass, FTransform);
子弹类生成
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ADrone::DoFire);
void ADrone::DoFire() { FTransform fireTrans = Mesh->GetSocketTransform(TEXT("Fire")); GetWorld()->SpawnActor<AMissle>(Bullet, fireTrans); }
敌人类生成
设置生成区域
SpawnArea = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea")); RootComponent = SpawnArea; SpawnArea->SetBoxExtent(FVector(1000.0f,1000.0f,100.0f));
生成
void AEnemySpawner::SpawnEnemy() { FVector origin; FVector extents; GetActorBounds(false, origin, extents); FVector spawnPoint = UKismetMathLibrary::RandomPointInBoundingBox(origin,extents); GetWorld()->SpawnActor<AEnemy>(EnemyType, spawnPoint,FRotator::ZeroRotator); }
设置开始生成
void AEnemySpawner::BeginPlay() { Super::BeginPlay(); GetWorld()->GetTimerManager().SetTimer(SpawnHandle, this,&AEnemySpawner::SpawnEnemy,spawnSpan,true); }
h:
#include "GameFramework/Actor.h" #include "Engine/EngineTypes.h" #include <LearningProject/Enemy.h>
UPROPERTY(VisibleAnywhere) class UBoxComponent* SpawnArea; UPROPERTY(EditAnywhere) TSubclassOf<AEnemy> EnemyType; UPROPERTY(EditAnywhere) float spawnSpan = 1.0f;
private: FTimerHandle SpawnHandle;
子弹攻击敌人判定
void AMissle::OverlapHandle(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { if (OtherActor->IsA<AEnemy>()) { OtherActor->Destroy(); this->Destroy(); } }
h:
UFUNCTION() void OverlapHandle(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
这篇关于【项目实训】UE4 C++ 物体生成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程
- 2024-12-20Svg Sprite Icon实战:从入门到上手的全面指南
- 2024-12-20LCD1602显示模块详解
- 2024-12-20利用Gemini构建处理各种PDF文档的Document AI管道