20uec++多人游戏【小球与玩家互动】
创始人
2024-03-04 12:52:21
0

为小球添加生命值组件和伤害处理函数

//生命值组件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(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());//设置要忽略的actorTArray 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(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(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,然后点击鼠标左键

 

将这两个设成4

 

这样可以让材质变得密集 

 

按住M,然后点击鼠标左键,得到乘节点,然后连线

 

按住1,然后点击鼠标左键,生成常数节点,然后连接到自发光上面

 这是将值设为0.5的效果

添加一个Time节点

 

 

然后按住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();}
}

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

现在加一下声音特效

添加两个成员变量

	//自爆中声音特效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());//设置要忽略的actorTArray 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(OtherActor);if (PlayerPawn){GetWorld()->GetTimerManager().SetTimer(TimerHandle_SelfDamage, this, &AASTrackerBot::DamageSelf, 0.5f, true , 0.0f);bStartSelfDamge = true;}//将自爆警告声音绑定到根组件UGameplayStatics::SpawnSoundAttached(SelfDestructSound, RootComponent);}
}

删掉这些成品,我们自己做一个

创建cue, cue实际上就是声音文件的子类

 

 

设置声音衰减

 

我们还可以做一个音效衰减

 

创建之后打开,设为自然音

这里选择衰减 

 

 

在蓝图中选择声音

 

然后添加一个声音组件,选择滚动声效

 

 

 编译测试成功  

相关内容

热门资讯

常用商务英语口语   商务英语是以适应职场生活的语言要求为目的,内容涉及到商务活动的方方面面。下面是小编收集的常用商务...
六年级上册英语第一单元练习题   一、根据要求写单词。  1.dry(反义词)__________________  2.writ...
复活节英文怎么说 复活节英文怎么说?复活节的英语翻译是什么?复活节:Easter;"Easter,anniversar...
2008年北京奥运会主题曲 2008年北京奥运会(第29届夏季奥林匹克运动会),2008年8月8日到2008年8月24日在中华人...
英语道歉信 英语道歉信15篇  在日常生活中,道歉信的使用频率越来越高,通过道歉信,我们可以更好地解释事情发生的...
六年级英语专题训练(连词成句... 六年级英语专题训练(连词成句30题)  1. have,playhouse,many,I,toy,i...
上班迟到情况说明英语   每个人都或多或少的迟到过那么几次,因为各种原因,可能生病,可能因为交通堵车,可能是因为天气冷,有...
小学英语教学论文 小学英语教学论文范文  引导语:英语教育一直都是每个家长所器重的,那么有关小学英语教学论文要怎么写呢...
英语口语学习必看的方法技巧 英语口语学习必看的方法技巧如何才能说流利的英语? 说外语时,我们主要应做到四件事:理解、回答、提问、...
四级英语作文选:Birth ... 四级英语作文范文选:Birth controlSince the Chinese Governmen...
金融专业英语面试自我介绍 金融专业英语面试自我介绍3篇  金融专业的学生面试时,面试官要求用英语做自我介绍该怎么说。下面是小编...
我的李老师走了四年级英语日记... 我的李老师走了四年级英语日记带翻译  我上了五个学期的小学却换了六任老师,李老师是带我们班最长的语文...
小学三年级英语日记带翻译捡玉... 小学三年级英语日记带翻译捡玉米  今天,我和妈妈去外婆家,外婆家有刚剥的`玉米棒上带有玉米籽,好大的...
七年级英语优秀教学设计 七年级英语优秀教学设计  作为一位兢兢业业的人民教师,常常要写一份优秀的教学设计,教学设计是把教学原...
我的英语老师作文 我的英语老师作文(通用21篇)  在日常生活或是工作学习中,大家都有写作文的经历,对作文很是熟悉吧,...
英语老师教学经验总结 英语老师教学经验总结(通用19篇)  总结是指社会团体、企业单位和个人对某一阶段的学习、工作或其完成...
初一英语暑假作业答案 初一英语暑假作业答案  英语练习一(基础训练)第一题1.D2.H3.E4.F5.I6.A7.J8.C...
大学生的英语演讲稿 大学生的英语演讲稿范文(精选10篇)  使用正确的写作思路书写演讲稿会更加事半功倍。在现实社会中,越...
VOA美国之音英语学习网址 VOA美国之音英语学习推荐网址 美国之音网站已经成为语言学习最重要的资源站点,在互联网上还有若干网站...
商务英语期末试卷 Part I Term Translation (20%)Section A: Translate ...