[UE4][C++]第一个简单的小游戏--黑洞引力

转载来源--大侠刘茗

转载地址-->https://zhuanlan.zhihu.com/p/41709219

项目原理如下:在黑洞外部是它的作用范围,主要是给游戏中的具有物理特性的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.全部完成后,再编译一下,然后运行程序,结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值