制作眩晕功能
添加眩晕标签
添加一个眩晕标签
cpp
CRUNCH_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(Stats_Stun)
cpp
UE_DEFINE_GAMEPLAY_TAG_COMMENT(Stats_Stun, "Stats.Stun", "眩晕")
使用GE来定制眩晕的时长
创建一个GE设置一个定长的眩晕GE
造成伤害的GE处添加眩晕GE
输入AbilitySystem.DebugAbilityTags就能看到赋予的Tag
添加对眩晕标签的处理,以及添加眩晕动画
到角色基类中添加对接收眩晕Tag的处理
cpp
#pragma region GAS组件相关
private:
// 眩晕标签的更新
void StunTagUpdated(const FGameplayTag Tag, int32 NewCount);
#pragma endregion
#pragma region 眩晕(Stun)
private:
UPROPERTY(EditDefaultsOnly, Category = "Stun")
UAnimMontage* StunMontage;
virtual void OnStun();
virtual void OnRecoverFromStun();
#pragma endregion
cpp
void ACCharacter::BindGASChangeDelegates()
{
if (CAbilitySystemComponent)
{
CAbilitySystemComponent->RegisterGameplayTagEvent(TGameplayTags::Stats_Dead).AddUObject(this, &ACCharacter::DeathTagUpdated);
CAbilitySystemComponent->RegisterGameplayTagEvent(TGameplayTags::Stats_Stun).AddUObject(this, &ACCharacter::StunTagUpdated);
}
}
void ACCharacter::StunTagUpdated(const FGameplayTag Tag, int32 NewCount)
{
if (IsDead()) return;
if (NewCount != 0)
{
OnStun();
PlayAnimMontage(StunMontage);
}else
{
OnRecoverFromStun();
StopAnimMontage(StunMontage);
}
}
void ACCharacter::OnStun()
{
}
void ACCharacter::OnRecoverFromStun()
{
}
在子类中重写获得眩晕标签和移除眩晕标签的函数
CPlayerCharacter
cpp
#pragma region 眩晕(Stun)
private:
virtual void OnStun();
virtual void OnRecoverFromStun();
#pragma endregion
眩晕了无法操作跟死亡一样禁用玩家的输入,解除眩晕的时候判断一下是否死亡,不然就会诈尸了
cpp
void ACPlayerCharacter::OnStun()
{
SetInputEnabledFromPlayerController(false);
}
void ACPlayerCharacter::OnRecoverFromStun()
{
if (IsDead()) return;
SetInputEnabledFromPlayerController(true);
}
另外还需要处理AI
cpp
// 监听Pawn眩晕Tag变化,控制AI逻辑启停
void PawnStunTagUpdated(const FGameplayTag Tag, int32 Count);
// 标记AI当前是否处于死亡状态
bool bIsPawnDead = false;
在死亡标签变化的时候修改布尔变量
cpp
void ACAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
UAbilitySystemComponent* PawnASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(InPawn);
if (PawnASC)
{
PawnASC->RegisterGameplayTagEvent(TGameplayTags::Stats_Dead, EGameplayTagEventType::NewOrRemoved).AddUObject(this, &ACAIController::PawnDeadTagUpdated);
PawnASC->RegisterGameplayTagEvent(TGameplayTags::Stats_Stun).AddUObject(this, &ACAIController::PawnStunTagUpdated);
}
}
void ACAIController::PawnDeadTagUpdated(const FGameplayTag Tag, int32 Count)
{
if (Count != 0)
{
GetBrainComponent()->StopLogic("Dead"); // 停止死亡状态下的逻辑
ClearAndDisableAllSenses(); // 清除感知数据
bIsPawnDead = true;
}
else
{
GetBrainComponent()->StartLogic(); // 重新启动AI逻辑
EnableAllSenses(); // 启用感知系统
bIsPawnDead = false;
}
}
void ACAIController::PawnStunTagUpdated(const FGameplayTag Tag, int32 Count)
{
if (bIsPawnDead) return;
if (Count != 0)
{
GetBrainComponent()->StopLogic("Stun");
}else
{
GetBrainComponent()->StartLogic();
}
}
在技能的基类中,从构造函数中添加阻止标签,眩晕不能用技能,再去被动技能中移除这个标签。
cpp
UCGameplayAbility::UCGameplayAbility()
{
// 眩晕状态无法激活技能
ActivationBlockedTags.AddTag(TGameplayTags::Stats_Stun);
}
cpp
UGAP_Launched::UGAP_Launched()
{
// 设置网络执行策略为仅在服务器端执行
NetExecutionPolicy = EGameplayAbilityNetExecutionPolicy::ServerOnly;
// 创建一个新触发数据对象
FAbilityTriggerData TriggerData;
// 设置触发数据的触发源为游戏事件
TriggerData.TriggerSource = EGameplayAbilityTriggerSource::GameplayEvent;
// 设置触发数据的触发标签为击飞被动技能激活标签
TriggerData.TriggerTag = GetLaunchedAbilityActivationTag();//TGameplayTags::Ability_Passive_Launch_Activate;
// 移除被击飞技能的禁用眩晕标签
ActivationBlockedTags.RemoveTag(TGameplayTags::Stats_Stun);
// 将创建的触发数据添加到能力触发器列表中
AbilityTriggers.Add(TriggerData);
}
添加小兵的死亡动画以及眩晕动画,依旧是关闭自动混出功能,设置一下对应的插槽
设置一下死亡动画,偏移时间调为0
再设置一下眩晕蒙太奇
设置一个机器人的眩晕蒙太奇
顺利的播放了蒙太奇动画