iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0
Software Development

30 天用 Unreal Engine 5 C++ 開發遊戲系列 第 9

# Day 9|修復近戰功能 (邏輯+動畫)

  • 分享至 

  • xImage
  •  

昨天太想睡了所以沒有做完整,今天來補全昨天有缺的基本攻擊功能。

1. Code修改

iThome30daysCharater.h

protected:
    void EndAttacj();
    FTimerHandle AttackTimerHandle;

iThome30daysCharacter.cpp

void AiThome30daysCharacter::DoAttack(UAnimMontage* AnimMontage)
{
	if (!bIsAttacking)
	{
		bIsAttacking = true;
		float duration = AnimMontage->GetSectionLength(0); 
        //這裡用 GetSectionLength 而不是 GetPlayLength 因為官方模板的 AnimMontage 是有3個區段的。
		PlayAnimMontage(AnimMontage);

		GetWorldTimerManager().ClearTimer(AttackTimerHandle);
		GetWorldTimerManager().SetTimer(AttackTimerHandle, this, &AiThome30daysCharacter::EndAttack, duration, false);
	}
}

void AiThome30daysCharacter::EndAttack()
{
	bIsAttacking = false;
}

https://ithelp.ithome.com.tw/upload/images/20250923/20171036rGeRTNdnZs.png

我 Code 裡說的 Section

2. Blueprint

https://ithelp.ithome.com.tw/upload/images/20250923/20171036lq96cVkAfT.png

3. Control Rig 修改

如果到第2點就結束的話,會發現腳都不會動,這是因為 Animation Blueprint 的 Control Rig 會讓腳黏在物體表面,所以這裡需要再 ABP 做修改:
把原本的 Chracter 改成 BP_ThirdPersonChracter,然後在左下 Component > Character 把 Character type 改成 iThome30daysChracter (你的C++專案Character)
https://ithelp.ithome.com.tw/upload/images/20250923/20171036GqyZNdjCvd.png
新增一個 Boolean IsAttacking,放在 Event Blueprint Update Animation 裡設為 BP_ThirdPersonChracter 的 Is Attacking
https://ithelp.ithome.com.tw/upload/images/20250923/20171036ohRTNBlqem.png
Anim Graph 裡把 Control Rig 的 Alpha 接上 isAttacking
https://ithelp.ithome.com.tw/upload/images/20250923/20171036xnJDhmiDfz.png

然後就能正常揮拳了!!

明天再來做 Combo 八...


上一篇
# Day 8|近戰攻擊邏輯
系列文
30 天用 Unreal Engine 5 C++ 開發遊戲9
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言