- 在UE4中新建C++类BlackHole
- 在.h文件中定义需要用到的组件,在这里实现的方式是利用重写函数的方法,定义需要用到的函数和变量,为BlackHole添加组件
#pragma once
#include \"CoreMinimal.h\"
#include \"Game work/Actor.h\"
#include \"Components/BoxComponent.h\"
#include \"Components/SphereComponent.h\"
#include \"PhysicsEngine/RadialForceComponent.h\"
#include \"BlackHole.generated.h\"
class UBoxComponent;
class UStaticMeshComponent;
class URadialForceComponent;
class USphereComponent;
UCLASS()
class TESTBLACKHOLE_API ABlackHole : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor\'s properties
ABlackHole();
protected:
//Create Components
UPROPERTY(VisibleAnywhere, Category = \"Components\")
UStaticMeshComponent*MeshComp;
UPROPERTY(VisibleAnywhere, Category = \"Components\")
UBoxComponent*BoxComp;
UPROPERTY(VisibleAnywhere, Category = \"Components\")
URadialForceComponent*ForceComp;
/*UPROPERTY(VisibleAnywhere, Category = \"Components\")
USphereComponent*SphereComp;
*/
//Marked with Ufunction to bind to overlap event
/*UFUNCTION()
void OverlapBox(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
*/
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every
virtual void Tick(float DeltaTime) override;
virtual void NotifyActorBeginOverlap(AActor* OtherActor)override;
};
- 在CPP文件中写上实现方法。
#include \"BlackHole.h\"
// Sets default values
ABlackHole::ABlackHole()
{
// Set this actor to call Tick() every . You can turn this off to improve performance if you don\'t need it.
PrimaryActorTick.bCanEverTick = true;
MeshComp = CreateDefaultSub <UStaticMeshComponent>(TEXT (\"MeshComp\"));
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RootComponent = MeshComp;
BoxComp = CreateDefaultSub <UBoxComponent>(TEXT(\"BoxComp\"));
BoxComp->SetupAttachment(RootComponent);
ForceComp = CreateDefaultSub <URadialForceComponent>(TEXT(\"ForceComp\"));
ForceComp->SetupAttachment(RootComponent);
SphereComp = CreateDefaultSub <USphereComponent>(TEXT(\"SphereComp\"));
SphereComp->SetupAttachment(RootComponent);
/*//Bind to Event
BoxComp->OnComponentBeginOverlap.AddDynamic(this,&ABlackHole::OverlapBox)
*/
}
/*
void ABlackHole::OverlapBox(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
OtherActor->Destroy();
}
}
*/
// Called when the game starts or when spawned
void ABlackHole::BeginPlay()
{
Super::BeginPlay();
//Bind to Event
}
// Called every
void ABlackHole::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
/*TArray<UPrimitiveComponent*>OverlappingComps;
SphereComp->GetOverlappingComponents(OverlappingComps);
for (int32 i=0; i<OverlappingComps.Num();i++)
{
UPrimitiveComponent* PrimComp = OverlappingComps[i];
if (PrimComp&&PrimComp->IsSimulatingPhysics())
{
const float SphereRadius = SphereComp->GetScaledSphereRadius();
const float ForceStrength = -2000;
PrimComp->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true);
}
}
*/
}
void ABlackHole::NotifyActorBeginOverlap(AActor* OtherActor)
{
Super::NotifyActorBeginOverlap(OtherActor);
AActor* ABlackHoleComps = Cast<AActor>(OtherActor);
if (ABlackHoleComps)
{
OtherActor->Destroy();
}
}
继续阅读与本文标签相同的文章
下一篇 :
如何用Python从海量文本抽取主题?
-
海南台风灾害影响评估三维模拟系统投入业务试运行
2026-05-18栏目: 教程
-
第六届世界互联网大会:实现5G网络全覆盖
2026-05-18栏目: 教程
-
网站不稳定和服务器没有关系么?
2026-05-18栏目: 教程
-
首座装配式3D打印“赵州桥”建成
2026-05-18栏目: 教程
-
新鲜升级,蚂蚁区块链为冷链发展保驾护航
2026-05-18栏目: 教程
