1. 在UE4中新建C++类BlackHole
  2. 在.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;
	
};

  1. 在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();

	}



}

  1. 这样的实现方式有些缺陷,就是不能让物体有位移和旋转的变化,具体效果
    视频播放
  2. 看了一些其他的写法,试着用了下,注释掉的部分就是,可能实现起来有一点儿小问题,可以参考
    添加链接描述
    可以选择阅读
    二.Add Dynamic事件不能正确调用
收藏 打印