87 lines
3.4 KiB
C#
87 lines
3.4 KiB
C#
using UnityEngine;
|
|
|
|
// 模擬 AI 視覺外掛的輔助瞄準 : 只用 EnemyPerceptor.Output (外掛真正拿到的結果)
|
|
// 模式與強度由 BotSpawner 每回合用 Apply() 套上 (自動配置的排程,或 StartUI 的設定) ; 0 = 無輔助,1 = 平滑跟槍
|
|
// 執行順序排在 PlayerLook 之前,修正量在同一幀套用
|
|
[DefaultExecutionOrder(-10)]
|
|
public class AimAssist : MonoBehaviour
|
|
{
|
|
[Header("參照")]
|
|
public EnemyPerceptor perceptor;
|
|
|
|
[Tooltip("CameraRoot 上的 PlayerLook")]
|
|
public PlayerLook playerLook;
|
|
|
|
[Header("平滑跟槍")]
|
|
[Tooltip("強度 1% / 100% 時的修正速度 (每秒修正剩餘誤差的比例係數,越大越快)")]
|
|
public Vector2 trackSpeed = new Vector2(2f, 30f);
|
|
|
|
// 給記錄器用 : 這一幀輔助做了什麼
|
|
public float FovRadius { get; private set; } // 作用範圍半徑 (度) : 有輔助固定 15,無輔助 0 (SessionPlan.AssistFovDeg)
|
|
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()
|
|
{
|
|
// 輔助模式與強度不在這裡讀 : 由 Apply() 每回合套上,才不會被第一回合的生成順序蓋掉
|
|
GameSettings s = GameSettings.Load();
|
|
noHorizontal = s.disableHorizontalAssist;
|
|
noVertical = s.disableVerticalAssist;
|
|
degreesPerCount = 0.022f * Mathf.Max(0.01f, (float)s.sensitivity); // 跟 PlayerLook 相同的換算
|
|
}
|
|
|
|
// 套用這一回合的條件,並清掉上一回合的殘留 (BotSpawner 在生成機器人、觸發 BotSpawned 之前呼叫)
|
|
public void Apply(SessionPlan.RoundCondition c)
|
|
{
|
|
mode = c.assistMode;
|
|
strength = c.assistStrength / 100f;
|
|
FovRadius = mode == 1 ? c.assistFovDeg : 0f; // 無輔助時記 0
|
|
remainingError = Vector2.zero;
|
|
lastSampleTime = -1f;
|
|
Engaged = false;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Engaged = false;
|
|
if (PauseMenu.IsPaused) return; // 暫停時不算、不累積
|
|
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;
|
|
}
|
|
}
|