Target Lock

兩個主要部分:「搜尋目標」與「維持鎖定(鏡頭追蹤)」。


1. 搜尋目標 (Acquire Target)

我們需要一個函數或事件來尋找鏡頭前方的敵人。

  • Logic: 使用 Sphere Trace By Profile

  • 關鍵參數:

    • Start: Camera 的 World Location。

    • End: Camera Forward Vector * 2000 + Start。

    • Radius: 150 (這個寬度能讓玩家比較容易「瞄準」到敵人)。


2. 核心鎖定邏輯 (The Logic Loop)

Tick 或一個定時器中,我們需要不斷更新控制器的旋轉,讓鏡頭死死盯著目標。

A. 計算旋轉 (Rotation Math)

使用 Find Look at Rotation 節點。

  • Start: 玩家的位置(Get Actor Location)。

  • Target: 鎖定目標的位置(Get Actor Location)。

  • 修正: 通常目標的 Location 是在腳底,建議加上一個 Z 軸偏移(例如 +100),這樣鏡頭會對準胸部。

B. 平滑追蹤 (Smooth Interp)

不要直接用 Set Control Rotation,那會非常生硬。使用 RInterp To 節點。

  • Current: Get Control Rotation。

  • Target: 剛才計算出的 LookAt Rotation。

  • Interp Speed: 建議 5.0 ~ 9.0。數值越高,鏡頭轉得越快。

實作過程問題

一開始使用 Sphere Trace By Channel 但是偵測不到敵人

一直偵測到地板

原因在 Collision 中 Sphere Trace By Channel 只會偵測 Visibility 有 Block 的物件

一直偵測到地板

後續改成 Sphere Trace By Profile 只偵測 object Type = Pawn 的方式

才能檢測到敵人

整體邏輯

  1. 點擊 tab 後透過 Sphere Trace By Profile 偵測 設定 camera 位置為起點 前方 *2000 為終點

  2. 如果碰撞到有 Enemy tag 就存起這個 Actor

  3. 在 Event Tick 中 如果有這個 Actor 就透過 set control Rotation 鎖定 camera

  4. 優化camera移動

1. 核心節點設置 (The Node Setup)

你需要在 Event Tick 或是透過 Timer 持續執行的邏輯中放置以下節點:

  • RInterp To: 這是插值核心。

    • Current: 接入 Get Control Rotation(從 Player Controller 取得)。

    • Target: 接入 Find Look at Rotation 的輸出。

    • Delta Time: 接入 Get World Delta Seconds

    • Interp Speed: 建議值 5.0 ~ 10.0。數值越大,鏡頭轉向越快、越「跟手」。

  • Set Control Rotation: 這是最終套用結果的節點,目標(Target)必須連到 Player Controller。


2. 處理「高度偏移」與「角度鎖定」 (關鍵優化)

為了防止鏡頭盯著腳底或是發生奇怪的傾斜(Roll),請務必按照下圖邏輯處理向量與轉向:

  1. 高度偏移:將敵人的 GetActorLocation 加上一個 Vector + Vector(Z 軸約 +80.0),作為 Find Look at Rotation 的 Target。

  2. 防止傾斜 (Break/Make Rotator):

    • RInterp To 的回傳值拉出 Break Rotator

    • 重新接入一個 Make Rotator

    • 只連接 Pitch (上下) 與 Yaw (左右),Roll (翻滾) 保持 0。這樣即便在斜坡上鎖定,畫面也不會歪掉。

Last updated