Input
為了實現完全平滑的移動和鏡頭控制,正確應用輸入訊號是必要的。以下章節將重點介紹基於滑鼠設備輸入資料的鏡頭查看旋轉。
獲取輸入
滑鼠的位移量可以透過以下方法獲取:
UnityEngine.Input.GetAxis("Mouse X")
UnityEngine.Input.GetAxisRaw("Mouse X")
UnityEngine.InputSystem.Mouse.current.delta.ReadValue()
以上所有方法都會返回未經後處理的滑鼠位移量。根據滑鼠的輪詢率(常見為125Hz,遊戲滑鼠通常為1000Hz),當移動滑鼠時,Mouse X
的值會呈現以下情況:




如你所見,每種情況都會產生不同的結果。在幾乎所有情況下,直接將這些值應用於鏡頭旋轉會導致視角移動不平滑。
平滑處理
為了實現完全平滑的視角控制,有兩種可能的解決方案:
- 滑鼠輪詢率、引擎更新率和螢幕刷新率必須一致——例如360Hz滑鼠輪詢率、360 FPS引擎更新率、360Hz螢幕刷新率。這並不現實。
- 輸入平滑處理——可以實現幾乎完美的平滑效果(即使在高階遊戲設備上也能明顯感受到差異),代價是增加幾毫秒的輸入延遲。
KCC 提供了一個實用指令碼Vector2Accumulator
。以下代碼展示了如何使用它來計算過去20毫秒內的值的平滑滑鼠位移量。
C#
public class PlayerInput : MonoBehaviour
{
// Creates an accumulator with 20ms smoothing window.
private Vector2Accumulator _lookRotationAccumulator = new Vector2Accumulator(0.02f, true);
private void Update()
{
Vector2 mouseDelta = Mouse.current.delta.ReadValue();
_lookRotationAccumulator.Accumulate(mouseDelta);
}
private void PollInput(CallbackPollInput callback)
{
Quantum.Input input = new Quantum.Input();
// 1. Option - consume whole smoothed mouse delta which is aligned to render time.
// input.LookRotationDelta = _lookRotationAccumulator.Consume();
// 2. Option (better) - consume smoothed mouse delta which is aligned to Quantum frame time.
// This variant ensures smooth interpolation when look rotation propagates to transform.
input.LookRotationDelta = _lookRotationAccumulator.ConsumeFrameAligned(callback.Game);
callback.SetInput(input, DeterministicInputFlags.Repeatable);
}
}
以下圖片展示了滑鼠位移量在不同輪詢率、引擎更新率和平滑窗口下傳播到 累積視角旋轉(約90°)的情況:
Purple
- 未應用平滑處理。Cyan
- 10毫秒平滑窗口。Green
- 20毫秒平滑窗口。Yellow
- 30毫秒平滑窗口。Blue
- 40毫秒平滑窗口。




對於一般使用者(滑鼠輪詢率125Hz),10-20毫秒的平滑窗口能在獲得平滑度和失去響應性之間取得良好的平衡。
對於擁有高性能遊戲設備的使用者(滑鼠輪詢率500+Hz,螢幕刷新率120+Hz),並達到高引擎更新率,建議使用3-5毫秒的平滑窗口,並提供完全關閉平滑處理的選項。
以下圖片展示了累積90°視角旋轉後的細節。

可以看到,應用平滑處理會增加平滑窗口長度的30-50%作為輸入延遲(使用10毫秒平滑窗口時,輸入延遲增加3.6毫秒)。
Back to top