iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
Software Development

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

# Day 4|修改 Character Movement Component 與 Sprint 功能

  • 分享至 

  • xImage
  •  

雖然這個系列的標題是 使用 C++ 來製作遊戲,而且的確在效能與大型專案上 C++ 比 Blueprint 更適合。
但在 初期設計雛形 或製作 簡單、不影響效能的功能 時,Blueprint 會更直覺也更快速。
實際上,兩者是 互補 的,並不是只能選其中一種方式。


🔎 Character Movement Component

在 UE5 的 Character 類別中,角色的移動邏輯主要透過 CharacterMovementComponent 控制。
https://ithelp.ithome.com.tw/upload/images/20250918/20171036C3fLsem1Nr.png

它提供了許多可調整的數值,如下圖:

https://ithelp.ithome.com.tw/upload/images/20250918/20171036zRAXizIGCF.png
我這還沒展開呢

這些數值既可以在 C++ 中修改,也能在 Blueprint 內直接調整。
例如在 C++ 裡:

GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->JumpZVelocity = 600.f;

🏃 Sprint 功能 (Blueprint 範例)

Sprint(衝刺)是一個常見的角色動作,我們可以用最簡單的方法來實作:

  1. 在資料匣中(預設Context\Input\Actions) 右鍵新增 Input Action 命名 IA_Sprint
    ~(方便以後可以新增客製化快捷鍵)~
    https://ithelp.ithome.com.tw/upload/images/20250918/20171036S1dOQDRaXk.png
  2. Content\Input 找到 IMC_Default,打開後在 Mappings 新增一個,在 asset 欄位選擇剛剛創建的 IA_Sprint 並設定按鍵。
    https://ithelp.ithome.com.tw/upload/images/20250918/201710366Luc7WK8bB.png
  3. 角色藍圖 (ThirdPersonCharacter)Event Graph 中新增一個 EnhancedInputAction IA_Sprint
  4. 新增 Get a Reference to self 接上 Character Movement (可以直接從旁邊抓下來),再接到 set MaxWalkSpeed
    在 按下 Shift 時,把 Max Walk Speed 改為 700。
    在 放開 Shift 時,把 Max Walk Speed 改回 500。
    https://ithelp.ithome.com.tw/upload/images/20250918/2017103680chdUe912.png
    我裝了 Darker Node 插件所以看起來跟原版不一樣

這樣就能快速得到一個可用的 Sprint 功能。


CharacterMovementComponent 提供角色移動的主要參數,能用來調整移動、跳躍、空中控制* Sprint 功能不一定要用 C++,Blueprint 就能在幾分鐘內完成。
在遊戲開發中,Blueprint 與 C++ 應該交替使用,各自發揮優勢。


明天會再新增一個翻滾或閃避動作,並嘗試用 C++ 實作輸入綁定。


上一篇
# Day 3|角色類別解析 與 API 初探
下一篇
# Day 5|閃避功能製作
系列文
30 天用 Unreal Engine 5 C++ 開發遊戲7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言