50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.InputSystem;
|
|
|
|
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()
|
|
{
|
|
if (Mouse.current != null)
|
|
{
|
|
Vector2 currentDelta = Mouse.current.delta.ReadValue();
|
|
|
|
if (smoothedDelta == Vector2.zero)
|
|
{
|
|
smoothedDelta = currentDelta;
|
|
}
|
|
else
|
|
{
|
|
smoothedDelta = (alpha * currentDelta) + ((1.0f - alpha) * smoothedDelta);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
smoothedDelta = Vector2.zero;
|
|
}
|
|
|
|
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}";
|
|
}
|
|
}
|
|
} |