- 在UE4中新建C++类BlackHole
- 在.h文件中定义需要用到的组件,在这里实现的方式是利用重写函数的方法,定义需要用到的函数和变量,为BlackHole添加组件
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/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 frame
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 frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT ("MeshComp"));
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RootComponent = MeshComp;
BoxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));
BoxComp->SetupAttachment(RootComponent);
ForceComp = CreateDefaultSubobject<URadialForceComponent>(TEXT("ForceComp"));
ForceComp->SetupAttachment(RootComponent);
SphereComp = CreateDefaultSubobject<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 frame
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();
}
}
版权声明:本文为weixin_44086537原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。