UE4 C ++ブラックホール効果



Ue4 C Black Hole Effects



****
.h
****

#pragma once #include 'CoreMinimal.h' #include 'GameFramework/Actor.h' #include 'MyActor.generated.h' UCLASS() class FPSGAME_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor() protected: / / Create a circular static mesh UPROPERTY(VisibleAnywhere, Category = 'Components') class UStaticMeshComponent* MyMeshcomp UPROPERTY(VisibleAnywhere,Category = 'Components') class USphereComponent* MySphereComp UPROPERTY(VisibleAnywhere, Category = 'Components') class USphereComponent* InnerSphereComponent UPROPERTY(VisibleAnywhere, Category = 'Components') class USphereComponent* OuterSphereComponet UFUNCTION() / / Set a function that Ue4 can recognize virtual void OverLapInnerShpere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) virtual void BeginPlay() override public: virtual void Tick(float DeltaTime) override }

****
.cpp
****



#include 'MyActor.h' #include 'Components/SphereComponent.h' #include 'Components/StaticMeshComponent.h' AMyActor::AMyActor() { PrimaryActorTick.bCanEverTick = true MyMeshcomp = CreateDefaultSubobject(TEXT('MyMeshcomp')) MyMeshcomp->SetCollisionEnabled(ECollisionEnabled::NoCollision) //Cancel the collision of objects RootComponent = MyMeshcomp //Set MyMeshcomp as the root component MySphereComp = CreateDefaultSubobject(TEXT('MySphereComp')) MySphereComp->SetupAttachment(MyMeshcomp) // MySphereComp is set to MyMeshcomp subcomponent InnerSphereComponent = CreateDefaultSubobject(TEXT('InnerSphereComponent')) InnerSphereComponent->SetSphereRadius(100) / / Set the radius InnerSphereComponent->SetupAttachment(MyMeshcomp) InnerSphereComponent->OnComponentBeginOverlap.AddDynamic(this,&AMyActor::OverLapInnerShpere) / / Bind function for OverlapComp HandleOverlap, the basic structure is the component name -> OnComponentBeginOverlap.AddDynamic (this, &A + the name of this file :: function name) ///SetSphereRadius SetSphereRadius changes the radius of the component OuterSphereComponet = CreateDefaultSubobject(TEXT('OuterSphereComponet')) OuterSphereComponet->SetSphereRadius(3000) OuterSphereComponet->SetupAttachment(MyMeshcomp) } void AMyActor::BeginPlay() { Super::BeginPlay() } void AMyActor::OverLapInnerShpere(UPrimitiveComponent* OverlappedComponent,AActor* OtherActor,UPrimitiveComponent* OtherComp,int32 OtherBodyIndex, bool bFromSweep,const FHitResult& SweepResult) { if (OtherActor) { OtherActor->Destroy() } } // Called every frame void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime) TArray OverlappingComps //UPrimitiveComponent refers to a class of components that can have transformations / / Repeatedly get overlapping components until the meta component is found, only valid for objects that support physical forces OuterSphereComponet->GetOverlappingComponents(OverlappingComps) //GetOverlappingComponents (Get overlapping components) / / Get the object that overlaps with itself, and set it as an array to get a type, and save as an array //OverlappingComps.Num() array length for (int32 i = 0 i IsSimulatingPhysics()) { const float SphereRadius = OuterSphereComponet->GetScaledSphereRadius() const float ForceStrength = -2000 / / Declare a variable of type Float PrimComp->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true) / / Apply a force to the object } } }

クラスを作成する

画像
画像



静的メッシュを追加します

画像

GenerateOverlapEvwntsを確認するには、特別な注意を払う必要があります



画像

結果を達成する

画像

参考まで