Files
MouseTrailCollector/Assets/Stript/InGame/Assist/AimAssist.cs
T
2026-10-24 10:39:10 +08:00

83 lines
2.9 KiB
C#

using UnityEngine;
// 模擬 AI 視覺外掛的輔助瞄準 : 只用 EnemyPerceptor.Output (外掛真正拿到的結果)
// 模式依 StartUI 設定 : 0 = 無輔助,1 = 平滑跟槍
public class AimAssist : MonoBehaviour
{
[Header("參照")]
public EnemyPerceptor perceptor;
[Tooltip("CameraRoot 上的 PlayerLook")]
public PlayerLook playerLook;
[Header("輔助範圍 (準心周圍一圈,半徑隨強度變大)")]
[Tooltip("強度 1% 時的半徑 (度)")]
public float minFov = 3f;
[Tooltip("強度 100% 時的半徑 (度)")]
public float maxFov = 15f;
[Header("平滑跟槍")]
[Tooltip("強度 1% / 100% 時的修正速度 (每秒修正剩餘誤差的比例係數,越大越快)")]
public Vector2 trackSpeed = new Vector2(2f, 30f);
// 給記錄器用 : 這一幀輔助做了什麼
public float FovRadius { get; private set; }
public bool Engaged { get; private set; }
private int mode;
private float strength; // 0~1
private bool noHorizontal;
private bool noVertical;
private float degreesPerCount;
private float lastSampleTime = -1f;
private Vector2 remainingError; // 平滑跟槍 : 這次取樣還沒修正完的誤差 (度)
void Start()
{
GameSettings s = GameSettings.Load();
mode = s.assistMode;
strength = s.assistStrength / 100f;
noHorizontal = s.disableHorizontalAssist;
noVertical = s.disableVerticalAssist;
degreesPerCount = 0.022f * Mathf.Max(0.01f, s.sensitivity); // 跟 PlayerLook 相同的換算
FovRadius = mode == 1 ? Mathf.Lerp(minFov, maxFov, strength) : 0f; // 無輔助時記 0
}
void Update()
{
Engaged = false;
if (mode != 1 || perceptor == null || playerLook == null) return;
EnemyPerceptor.Detection d = perceptor.Output;
Vector2 error = d.angleError;
if (noHorizontal) error.x = 0f;
if (noVertical) error.y = 0f;
bool inRange = d.detected && d.angleError.magnitude <= FovRadius;
SmoothTrack(d, error, inRange);
}
// 平滑跟槍 : 每次拿到新的辨識結果,就把那時候的誤差一點一點補到滑鼠上
// 外掛只知道擷取當下的誤差,所以兩次辨識之間不會更新 (延遲越大越容易過頭)
void SmoothTrack(EnemyPerceptor.Detection d, Vector2 error, bool inRange)
{
if (!Mathf.Approximately(d.captureTime, lastSampleTime))
{
lastSampleTime = d.captureTime;
remainingError = inRange ? error : Vector2.zero;
}
if (remainingError == Vector2.zero) return;
float speed = Mathf.Lerp(trackSpeed.x, trackSpeed.y, strength);
Vector2 step = remainingError * (1f - Mathf.Exp(-speed * Time.deltaTime));
remainingError -= step;
playerLook.AddAssistDelta(step / degreesPerCount); // 度 → 滑鼠單位
Engaged = true;
}
}