iT邦幫忙

2025 iThome 鐵人賽

DAY 5
0

我想要做像 Elden Ring 一樣,可以讓角色快速移動、迴避攻擊的功能。
今天我們會透過 C++ 和 Blueprint 組合,新增一個簡單的 Dodge 機制。


Step 1. 在 C++ 裡新增 Dodge 狀態

iThome30daysCharacter.h 裡新增 Dodge 相關的
變數 :

public:
    UPROPERTY(Category="Character: State", EditAnywhere, BlueprintReadWrite)
    bool bIsSprinting;
	
    UPROPERTY(Category="Character: State", EditAnywhere, BlueprintReadWrite)
    bool bIsDodging;
	
    UPROPERTY(Category="Character Movement: Dodge", EditAnywhere)
    float DodgeDuration;
	
    UPROPERTY(Category="Character Movement: Dodge", EditAnywhere)
    float LastShiftTime;

    UPROPERTY(Category="Character Movement: Dodge", EditAnywhere)
    float DoubleTapThreshold; // 0.25秒內按兩次才算

protected:
    FTimerHandle DodgeTimerHandle;

UPROPERTY(EditAnywhere, BlueprintReadWrite) 讓變數能夠在藍圖裡被編輯。

函式 :

public:
    UFUNCTION(BlueprintCallable, Category="Input")
    virtual void DoDodge();

protected:
    void StartDodge();
    void EndDodge();

UFUNCTION(BlueprintCallable) 讓功能能夠在藍圖裡使用。


2. 在 C++ 裡實作 Dodge

iThome30daysCharacter.cpp 加上:

Dodge判斷

void AiThome30daysCharacter::DoDodge()
{
    float CurrentTime = GetWorld()->GetTimeSeconds();

    if (LastShiftTime > 0 && (CurrentTime - LastShiftTime) <= DoubleTapThreshold)
    {
        StartDodge(); // 觸發 Dodge
        LastShiftTime = -1.0f; // reset
    }
    else
    {
        LastShiftTime = CurrentTime;
    }
}

因為我想做 Elden Ring 那樣雙擊 Shift 來執行閃避,所以這裡加了雙擊判定

開始 / 停止閃避

void AiThome30daysCharacter::StartDodge()
{
    if (bIsDodging) return;
    bIsDodging = true;
    GetWorldTimerManager().SetTimer(DodgeTimerHandle, this, &AiThome30daysCharacter::EndDodge, DodgeDuration, false);
    
}
void AiThome30daysCharacter::EndDodge()
{
    bIsDodging = false;
}

因為之後要用 **Root Motion Animation** 來做閃避動畫,所以這裡沒有加任何角色動量修改。


在 Blueprint 綁定 Dodge Input

接下來切換到 Blueprint (ThirdPersonCharacter Event Graph),將 Shift 綁定到 DoDodge()

https://ithelp.ithome.com.tw/upload/images/20250919/20171036SG69d93mCh.png

4. 測試結果

  • 連按 Shift 兩次 → Dodging state 會變為true。
  • Dodge 中不可再觸發 Dodge,避免無限迴避。
  • 可以調整 DoubleTapThresholdDodgeDuration 來改變 雙擊間隔 和 閃避持續時間 (下次用 閃避動畫的長度來設置)。


明天我們會開始研究 動畫系統,讓 閃避動作能搭配正確的動畫播放。


上一篇
# Day 4|修改 Character Movement Component 與 Sprint 功能
下一篇
# Day 6|加入 Root Motion 閃避動畫
系列文
30 天用 Unreal Engine 5 C++ 開發遊戲7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言