Files

43 lines
1.2 KiB
C#

using UnityEngine;
using TMPro;
public class Movement : MonoBehaviour
{
[Header("UI 參照")]
[Tooltip("Text (TMP)")]
public TextMeshProUGUI movementVectorText;
private Vector2 smoothedDelta = Vector2.zero;
private const float alpha = 0.1f;
[Header("顯示")]
[Tooltip("文字更新間隔 (秒),計算仍然每幀進行")]
public float updateInterval = 0.5f;
private float displayTimer = 0f;
void Update()
{
// 跟遊戲內視角讀同一份 Raw Input 的位移
Vector2 currentDelta = RawMouseInput.FrameDelta;
if (smoothedDelta == Vector2.zero)
{
smoothedDelta = currentDelta;
}
else
{
smoothedDelta = (alpha * currentDelta) + ((1.0f - alpha) * smoothedDelta);
}
displayTimer += Time.unscaledDeltaTime;
if (displayTimer < updateInterval) return;
displayTimer = 0f;
if (movementVectorText != null)
{
string xStr = (smoothedDelta.x >= 0 ? "+" : "") + smoothedDelta.x.ToString("F1");
string yStr = (smoothedDelta.y >= 0 ? "+" : "") + smoothedDelta.y.ToString("F1");
movementVectorText.text = $"移動向量: X: {xStr}, Y: {yStr}";
}
}
}