当前位置: 首页 > news >正文

RPG34.AI攻击

1.打开XMBABilitySystemComponent.h ,添加一个激活函数用于激活攻击

	UFUNCTION(BlueprintCallable, Category = "XMB|Ability")bool TryActivateAbilityByTag(FGameplayTag AbilityTagToActivate);
bool UXMBAbilitySystemComponent::TryActivateAbilityByTag(FGameplayTag AbilityTagToActivate)
{check(AbilityTagToActivate.IsValid());//用于存储GameplayAbilitiesTArray<FGameplayAbilitySpec*> FoundABilitySpecs;//获取所有匹配标签的GameplaySpecGetActivatableGameplayAbilitySpecsByAllMatchingTags(AbilityTagToActivate.GetSingleTagContainer(),FoundABilitySpecs);if (!FoundABilitySpecs.IsEmpty()){const int32 RandomAbilityIndex = FMath::RandRange(0,FoundABilitySpecs.Num() - 1);FGameplayAbilitySpec* SpecToActivate = FoundABilitySpecs[RandomAbilityIndex];check(SpecToActivate);if (!SpecToActivate->IsActive()){return TryActivateAbility(SpecToActivate->Handle);}}return false;
}

2.打开项目,先制作一个Ability

然后创建子类

3.打开EnemyCombatComponent.h,覆写一个函数

public:virtual void OnHitTargetActor(AActor* HitActor) override ;
void UEnemyCombatComponent::OnHitTargetActor(AActor* HitActor)
{if (OverlappedActors.Contains(HitActor)){return;}OverlappedActors.AddUnique(HitActor);//TODO: Implement block checkbool bIsValidBlock = false;const bool bIsPlayerBlocking = false;const bool bIsMyAttackUnblockble = false;if (bIsPlayerBlocking && !bIsMyAttackUnblockble){//TODO:check if the block is valid}FGameplayEventData EventData;EventData.Instigator = GetOwningPawn();EventData.Target = HitActor;if (bIsValidBlock){//TODO:Handle successful block}else{UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(GetOwningPawn(),XMBGameplayTags::Shared_Event_MeleeHit,EventData);}
}

打开XMBWarriorFunctionLibrary.h

//检测是否为可攻击目标UFUNCTION(BlueprintPure, Category = "XMBWorrior|FunctionLibrary")static bool IsTargetPawnHostile(APawn* QueryPawn, APawn* TargetPawn);
bool UXMBWarriorFunctionLibrary::IsTargetPawnHostile(APawn* QueryPawn, APawn* TargetPawn)
{check(QueryPawn && TargetPawn);IGenericTeamAgentInterface* QueryTeamAgent = Cast<IGenericTeamAgentInterface>(QueryPawn->GetController());IGenericTeamAgentInterface* TargetTeamAgent = Cast<IGenericTeamAgentInterface>(TargetPawn->GetController());if (QueryTeamAgent && TargetTeamAgent){return QueryTeamAgent->GetGenericTeamId() != TargetTeamAgent->GetGenericTeamId();}return false;
}

打开XMBWeaponbase.h,为了确保player和enemy在攻击的时候不会对友军造成伤害或者在一次攻击内对对方造成多段伤害,修改OnCollisionBoxBeginOverlap与OnCollisionEndOverlap函数

void AXMBWeaponBase::OnCollisionBoxBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{//APawn* WeaponOwningPawn = GetInstigator<APawn>();checkf(WeaponOwningPawn,TEXT("forgot to assign an instagtor as the owning pawn of the weapon : %s"),* GetName());if (APawn* HitPawn = Cast<APawn>(OtherActor)){if (UXMBWarriorFunctionLibrary::IsTargetPawnHostile(WeaponOwningPawn,HitPawn))//命中的对象与武器装备者本身不相同{OnWeaponHitTarget.ExecuteIfBound(OtherActor);}}
}void AXMBWeaponBase::OnCollisionEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{//APawn* WeaponOwningPawn = GetInstigator<APawn>();checkf(WeaponOwningPawn,TEXT("forgot to assign an instagtor as the owning pawn of the weapon : %s"),* GetName());if (APawn* HitPawn = Cast<APawn>(OtherActor)){if (UXMBWarriorFunctionLibrary::IsTargetPawnHostile(WeaponOwningPawn,HitPawn))//命中的对象与武器装备者本身不相同{OnWeaponPulledFromTarget.ExecuteIfBound(OtherActor);}}
}

敌人在造成伤害的时候,要通过EffectSpecHandle进行。所以在WarriorEnemyGameplayAbility内进行设置

UFUNCTION(BlueprintPure, Category = "Warrior|Ability")FGameplayEffectSpecHandle MakeEnemyDamageEffectSpecHandle(TSubclassOf<UGameplayEffect> EffectClass,const FScalableFloat& InDamageScalableFloat);
FGameplayEffectSpecHandle UWarriorEnemyGameplayAbility::MakeEnemyDamageEffectSpecHandle(TSubclassOf<UGameplayEffect> EffectClass,const FScalableFloat& InDamageScalableFloat)
{check(EffectClass);//创建HandleFGameplayEffectContextHandle ContextHandle = XMBGetAbilitySystemComponentFromActorInfo()->MakeEffectContext();ContextHandle.SetAbility(this);//设置Ability实例ContextHandle.AddSourceObject(GetAvatarActorFromActorInfo());//设置源对象ContextHandle.AddInstigator(GetAvatarActorFromActorInfo(),GetAvatarActorFromActorInfo());//在这一步指定ability的使用者和ability的拥有者FGameplayEffectSpecHandle EffectSpecHandle = XMBGetAbilitySystemComponentFromActorInfo()->MakeOutgoingSpec(EffectClass,GetAbilityLevel(),ContextHandle);EffectSpecHandle.Data->SetSetByCallerMagnitude(XMBGameplayTags::Shared_SetByCaller_BaseDamage,InDamageScalableFloat.GetValueAtLevel(GetAbilityLevel()));return EffectSpecHandle;
}

4。打开之前创建好的abiity,进行修改

然后打开CT——GuardianStates,添加一条曲线

 创建两个Ability的子类

创建好蒙太奇后,添加动画状态通知

相关文章:

  • 音视频之H.264的可伸缩编码SVC
  • AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月20日第114弹
  • el-tree的属性render-content自定义样式不生效
  • linux——C程序的编译与调试
  • Kotlin 中ArrayList、listOf、arrayListOf 和 mutableListOf区别
  • 学习路之uniapp--uniapp扩展uni-ui
  • 常用绘图工具网站推荐合集:打造高效可视化表达力!
  • 亚马逊云科技中国峰会
  • 如何在服务器终端下载百度网盘数据
  • 搭建pikachu靶场
  • 礼让行人:深圳 vs 上海
  • tkinter 的 pack() 布局管理器学习指南
  • AI 时代,数仓建模还有价值吗?
  • 第32周———Tensorflow|LSTM-火灾温度预测
  • 【unity游戏开发——热更新】什么是Unity热更新
  • AI觉醒:从实验室幻影到社会氧气——一部穿透60年的智能进化史诗
  • Vue 比较两个数组对象,页面展示差异数据值
  • 算法-每日一题(DAY10)打家劫舍
  • 关于Seata的一个小issue...
  • 基于大模型的三叉神经痛预测及治疗方案研究报告
  • 个人网站整站源码下载/站点推广是什么意思
  • 宜春seo网站推广/长沙靠谱seo优化
  • 衡水企业做网站推广/广告营销
  • 优化前网站现状分析/上海最新发布最新
  • 小型电商网站模板/网络营销比较常用的营销模式
  • asp.net网站的404错误页面/郑州网站建设推广有限公司