UE4利用C++实现黑洞吸物效果

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

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

	}



}

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

版权声明:本文为weixin_44086537原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。