**
一.源代码
**
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FPSBlackHold.generated.h"
class USphereComponent;
class UStaticMeshComponent;
UCLASS()
class FPSGAME_API AFPSBlackHold : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFPSBlackHold();
protected:
UPROPERTY(VisibleAnywhere, Category = "Components")
UStaticMeshComponent* MeshComp;
//内部球体破坏了重叠的组件
UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* InnerSphereComponent;//ZX内层球体
UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* OuterSphereComponent;//ZX外层球体
//标记为ufunction绑定到重叠事件
UFUNCTION()
void OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherAActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSBlackHold.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AFPSBlackHold::AFPSBlackHold()
{
// 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;
InnerSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("InnerSphereComp"));
InnerSphereComponent->SetSphereRadius(100);
InnerSphereComponent->SetupAttachment(MeshComp);
//绑定重叠事件函数(用于销毁盒体的函数)
InnerSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AFPSBlackHold::OverlapInnerSphere);
OuterSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("OuterSphereComp"));
OuterSphereComponent->SetSphereRadius(3000);//覆盖整个关卡的球体
OuterSphereComponent->SetupAttachment(MeshComp);
}
void AFPSBlackHold::OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{ //找出重叠的并销毁
if (OtherActor)
{
OtherActor->Destroy();
}
}
// 称每一帧
void AFPSBlackHold::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//找到所有可能发生碰撞的重叠组件,并可能进行物理模拟
TArray<UPrimitiveComponent*>OverlappingComps;//ZXprimitive元组件
//物理作用力的对象
OuterSphereComponent->GetOverlappingComponents(OverlappingComps);
for (int32 i = 0; i < OverlappingComps.Num(); i++)
{
UPrimitiveComponent* PrimComp = OverlappingComps[i];
if (PrimComp && PrimComp->IsSimulatingPhysics())
{
//我们正在寻找的组件!它需要模拟才能施加外力。
const float SphereRadius = OuterSphereComponent->GetScaledSphereRadius();
const float ForceStrength = -2000;//负的价值使它向原点而不是推开
//作用力强度
PrimComp->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true);
//施加作用力(径向力)
}
}
}
二 . BlackHole Material
三 . 导入材质
四 . 为所有Actor勾选Generate Overlap Events 生成重叠事件