转载来源--大侠刘茗
转载地址-->https://zhuanlan.zhihu.com/p/
项目原理如下:在黑洞外部是它的作用范围,主要是给游戏中的具有物理特性的Actor加一个径向力,是它飞向黑洞。
在黑洞的内部,会触发一个函数,删除当前进入黑洞的Actor。于是一个黑洞小游戏就完成了。
1.新建UE4工程(第三人称工程,c++版本),取名BlackHole
2.添加新项-->新建C++类
3.选择父类,Actor,继续

4.取名BlackHoleActor,选择公有(注意公有之后的文件位置就不一样了,这个后面说) ,创建,等待vs部分创建完成

5.h文件
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "BlackHoleActor.generated.h" class USphereComponent; class UStaticMeshComponent; UCLASS() class BLACKHOLE_API ABlackHoleActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties ABlackHoleActor(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; UPROPERTY(VisibleAnywhere, Category = "Components") UStaticMeshComponent* MeshComp; /*物体碰到黑洞表面会被销毁*/ UPROPERTY(VisibleAnywhere, Category = "Components") USphereComponent* InnerSphereComponent; /*黑洞的作用范围*/ UPROPERTY(VisibleAnywhere, Category = "Components") USphereComponent* OuterSphereComponent; /*重叠事件*/ UFUNCTION() void OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); public: // Called every frame virtual void Tick(float DeltaTime) override; };
讯享网
6.cpp文件
讯享网// Fill out your copyright notice in the Description page of Project Settings. //注意这里头文件路径,因为公有了所以系统自动生成的不要了更改如下 #include "../Public/BlackHoleActor.h" #include "Components/SphereComponent.h" #include "Components/StaticMeshComponent.h" #include "Engine.h" // Sets default values ABlackHoleActor::ABlackHoleActor() { // 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")); //取消黑洞Mesh的碰撞,使黑洞可以吸入物体 MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision); RootComponent = MeshComp; InnerSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("InnerSphereComp")); InnerSphereComponent->SetSphereRadius(100); InnerSphereComponent->SetupAttachment(MeshComp); //绑定重叠事件,使吸入的物体被销毁 InnerSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ABlackHoleActor::OverlapInnerSphere); OuterSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("OuterSphereComp")); OuterSphereComponent->SetSphereRadius(3000); OuterSphereComponent->SetupAttachment(MeshComp); } // Called when the game starts or when spawned void ABlackHoleActor::BeginPlay() { Super::BeginPlay(); } void ABlackHoleActor::OverlapInnerSphere(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) { if (OtherActor) { GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Red, FString::Printf(TEXT("a actor is died!"))); OtherActor->Destroy(); } } // Called every frame void ABlackHoleActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); TArray<UPrimitiveComponent*> OverlappingComps; OuterSphereComponent->GetOverlappingComponents(OverlappingComps); for (int32 i = 0; i < OverlappingComps.Num(); i++) { UPrimitiveComponent* PrimComp = OverlappingComps[i]; //检查是否模拟物理 if (PrimComp && PrimComp->IsSimulatingPhysics()) { // the component we are looking for! It needs to be simulating in order to apply forces. const float SphereRadius = OuterSphereComponent->GetScaledSphereRadius(); // Negative value to make it pull towards the origin instead of pushing away const float ForceStrength = -2000; //添加径向力 PrimComp->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true); } } }
7.进入UE4,编译

8.编译成功后,新建蓝图类,选择BlackHole,确定

9.命名,然后将蓝图类拖入游戏内,接下来就是显示出来
10.点击刚才拖入的蓝图类,点击 细节中的 添加组件,选择球体

11.更改外观,选择basecolor

12.现在开始创建多个Actor,用来展示黑洞的威力
13.添加新项-->蓝图类-->Actor,命名,然后将Actor 拖入游戏中,和黑洞一样添加组件将其显示出来。
14.给Actor添加物理属性,双击Actor,在细节栏中找到Simulate Physics,然后勾选

15.全部完成后,再编译一下,然后运行程序,结果如下


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/53442.html