20uec++ マルチプレイヤー ゲーム [ボールがプレイヤーと相互作用する]

ヘルス コンポーネントとダメージ ハンドラーをボールに追加する

//生命值组件
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
	class USHealthComponent * HealthComp;

	//伤害处理函数
	UFUNCTION()
	void HandleTakeAnyDamage(USHealthComponent * OwnerHealthComp, float Health, float HealthDelta, const UDamageType * DamageType, AController * InstigatedBy, AActor * DamageCauser);

コンポーネントの作成と関数の定義

HealthComp = CreateDefaultSubobject<USHealthComponent>(TEXT("HealthComp"));
void AASTrackerBot::HandleTakeAnyDamage(USHealthComponent * OwnerHealthComp, float Health, float HealthDelta, const UDamageType * DamageType, AController * InstigatedBy, AActor * DamageCauser)
{
	
	UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(HealthComp->Health));

}

爆発関連のメンバー変数と関数を追加する

	//自爆函数
	void SelfDestruct();

	//爆炸特效
	UPROPERTY(EditDefaultsOnly, Category = "TracerBot")
	class UParticleSystem * ExplorEffect;

	//是否已经爆炸
	bool bExplored;

	//爆炸半径
	UPROPERTY(EditDefaultsOnly, Category = "TracerBot")
	float ExplorRadius;

	//爆炸伤害
	UPROPERTY(EditDefaultsOnly, Category = "TracerBot")
	float ExplorDamage;

コンストラクタで初期化する

	bExplored = false;
	ExplorRadius = 200;
	ExplorDamage = 100;

自己破壊関数を定義する

void AASTrackerBot::SelfDestruct()
{
	//检查是否已经爆炸了
	if (bExplored)
	{
		return;
	}
	bExplored = true;
	//发生爆炸
	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplorEffect, GetActorLocation());
	//设置要忽略的actor
	TArray<AActor * > IgnoredActors;
	IgnoredActors.Add(this);

	//对自身进行伤害
	UGameplayStatics::ApplyRadialDamage(this, ExplorDamage, GetActorLocation(), ExplorRadius, nullptr, IgnoredActors, this, GetInstigatorController(), true);

	//画出伤害范围
	DrawDebugSphere(GetWorld(), GetActorLocation(), ExplorRadius, 12, FColor::Green, false, 2.0f, 0, 1.0f);
	//自毁
	Destroy();
}

ダメージ処理では、この関数を呼び出します

void AASTrackerBot::HandleTakeAnyDamage(USHealthComponent * OwnerHealthComp, float Health, float HealthDelta, const UDamageType * DamageType, AController * InstigatedBy, AActor * DamageCauser)
{
	
	UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(HealthComp->Health));
	if (HealthComp->Health <= 0)
	{
		SelfDestruct();
	}
}

===========================================

プレイヤーに近づいたときのタイミング自己爆発機能を実装しました

関連する変数と関数を作成する

	//球形碰撞组件
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
	class USphereComponent* CollisionComponent;

	//自爆倒计时句柄
	FTimerHandle TimerHandle_SelfDamage;

	//倒计时自爆函数
	void DamageSelf();

	//是否已经开始自毁倒计时
	bool bStartSelfDamge;

	//游戏重叠函数
	virtual void NotifyActorBeginOverlap(AActor * OtherActor) override;

コンストラクターで球状コンポーネントを作成し、関連するパラメーターを設定します

	CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
	CollisionComponent->SetSphereRadius(200);
	CollisionComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	CollisionComponent->SetCollisionResponseToAllChannels(ECR_Ignore);
	CollisionComponent->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
	CollisionComponent->SetupAttachment(RootComponent);

カウントダウン自己破壊関数を定義する

void AASTrackerBot::DamageSelf()
{
	UGameplayStatics::ApplyDamage(this, 20, GetInstigatorController(), this, nullptr);
}

オーバーラップ関数を定義する

void AASTrackerBot::NotifyActorBeginOverlap(AActor * OtherActor)
{
	if (!bStartSelfDamge)
	{
		ASCharacter * PlayerPawn = Cast<ASCharacter>(OtherActor);
		if (PlayerPawn)
		{
			GetWorld()->GetTimerManager().SetTimer(TimerHandle_SelfDamage, this, &AASTrackerBot::DamageSelf, 0.5f, true , 0.0f);
			bStartSelfDamge = true;
		}
	}
}

コンパイルしてから、ボールの設計図を開き、特殊効果を選択します

コンパイルしてテストし、ボールが爆発しないことを確認します。

コンストラクタのイベントバインディング関数をゲーム開始関数に入れます。それでおしまい

// Called when the game starts or when spawned
void AASTrackerBot::BeginPlay()
{
	Super::BeginPlay();
	NextPathPoint = GetNextPathPoint();
	//伤害事件绑定函数
	HealthComp->OnHealthChanged.AddDynamic(this, &AASTrackerBot::HandleTakeAnyDamage);
}

 どうしてか分かりません。

人を殺すこともできます。

========================================

では、ボールの素材を改良しましょう

 T を押したまま、マウスの左ボタンをクリックします。

次に素材を選びます

 

U を押してから、マウスの左ボタンをクリックします。

 

この 2 つを 4 に設定します

 

これにより、材料の密度が高くなります 

 

M を押しながらマウスの左ボタンをクリックして乗算ノードを取得し、接続します。

 

1 を押したままマウスの左ボタンをクリックして定数ノードを生成し、それを自己照明に接続します。

 これは、値を 0.5 に設定した効果です。

時間ノードを追加する

 

 

次に、s を押したままマウスの左ボタンをクリックして、パラメータ ノードを生成します。 

 (時間 - パラメータ) * 4 を使用して、積を制約します。

 次に、1-上記の結果

次に、インデックスを取得します

それを自己照明に割り当てます

 ボール クラスに戻り、ボールのマテリアルの変数を追加します。

	//动态材质
	class UMaterialInstanceDynamic * MatInst;
#include "Materials/MaterialInstanceDynamic.h"

ダメージ処理機能の向上

void AASTrackerBot::HandleTakeAnyDamage(USHealthComponent * OwnerHealthComp, float Health, float HealthDelta, const UDamageType * DamageType, AController * InstigatedBy, AActor * DamageCauser)
{
	//UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(HealthComp->Health));

	//得到MeshComponent组件的材质
	if (MatInst == nullptr)
	{
		MatInst = MeshComponent->CreateAndSetMaterialInstanceDynamicFromMaterial(0, MeshComponent->GetMaterial(0));
		UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(HealthComp->Health));
	}
	//将参数设为当前时间,让材质发光
	if (MatInst)
	{
		MatInst->SetScalarParameterValue("LastTimeDamageTaken", GetWorld()->TimeSeconds);
	}
	//自爆
	if (HealthComp->Health <= 0)
	{
		
		SelfDestruct();
	}
}

==========================================

今すぐ効果音を追加してください

2 つのメンバー変数を追加する

	//自爆中声音特效
	UPROPERTY(EditDefaultsOnly, Category = "TracerBot")
	class USoundCue * SelfDestructSound;

	//爆炸特效
	UPROPERTY(EditDefaultsOnly, Category = "TracerBot")
	class USoundCue * ExploedSound;
#include "Sound/SoundCue.h"

SelfDestruct 関数と NotifyActorBeginOverlap 関数を更新します。

void AASTrackerBot::SelfDestruct()
{
	//检查是否已经爆炸了
	if (bExplored)
	{
		return;
	}
	bExplored = true;
	//发生爆炸
	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplorEffect, GetActorLocation());
	//设置要忽略的actor
	TArray<AActor * > IgnoredActors;
	IgnoredActors.Add(this);
//	UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(222.0f));

	//对自身进行伤害
	UGameplayStatics::ApplyRadialDamage(this, ExplorDamage, GetActorLocation(), ExplorRadius, nullptr, IgnoredActors, this, GetInstigatorController(), true);

	//画出伤害范围
	DrawDebugSphere(GetWorld(), GetActorLocation(), ExplorRadius, 12, FColor::Green, false, 2.0f, 0, 1.0f);
	//发生爆炸声,在actor的位置
	UGameplayStatics::PlaySoundAtLocation(this, ExploedSound, GetActorLocation());
	//自毁
	Destroy();
}
void AASTrackerBot::NotifyActorBeginOverlap(AActor * OtherActor)
{
	if (!bStartSelfDamge)
	{
		ASCharacter * PlayerPawn = Cast<ASCharacter>(OtherActor);
		if (PlayerPawn)
		{
			GetWorld()->GetTimerManager().SetTimer(TimerHandle_SelfDamage, this, &AASTrackerBot::DamageSelf, 0.5f, true , 0.0f);
			bStartSelfDamge = true;
		}

		//将自爆警告声音绑定到根组件
		UGameplayStatics::SpawnSoundAttached(SelfDestructSound, RootComponent);
	}
}

これらの完成品を削除して、自分で作ろう

キューを作成します。キューは実際にはサウンドファイルのサブクラスです

 

 

音の減衰を設定する

 

消音もできます

 

作成後に開き、自然音に設定

ここで減衰を選択します 

 

 

ブループリントでサウンドを選択

 

次に、サウンドコンポーネントを追加し、スクロール効果音を選択します

 

 

 コンパイルテスト成功  

おすすめ

転載: blog.csdn.net/zhang2362167998/article/details/128097328