110 lines
4.1 KiB
C#
110 lines
4.1 KiB
C#
using UnityEngine;
|
|
|
|
// 模擬 AI 視覺外掛的輔助瞄準 : 只用 EnemyPerceptor.Output (外掛真正拿到的結果)
|
|
// 模式依 StartUI 設定 : 0 = 無輔助,1 = 平滑跟槍,2 = 動態靈敏度
|
|
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);
|
|
|
|
[Header("動態靈敏度 (沒有範圍限制,偵測到就生效)")]
|
|
[Tooltip("強度 100% 時,準心貼在敵人身上的靈敏度倍率")]
|
|
[Range(0f, 1f)] public float minSensitivityScale = 0.1f;
|
|
|
|
[Tooltip("離胸口這個角度以內都維持最低靈敏度 (度),大約是身體的寬度")]
|
|
public float innerAngle = 2f;
|
|
|
|
// 給記錄器用 : 這一幀輔助做了什麼
|
|
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 = Mathf.Lerp(minFov, maxFov, strength);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Engaged = false;
|
|
if (mode == 0 || 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;
|
|
|
|
if (mode == 1) SmoothTrack(d, error, inRange);
|
|
else if (mode == 2) DynamicSensitivity(d);
|
|
}
|
|
|
|
// 平滑跟槍 : 每次拿到新的辨識結果,就把那時候的誤差一點一點補到滑鼠上
|
|
// 外掛只知道擷取當下的誤差,所以兩次辨識之間不會更新 (延遲越大越容易過頭)
|
|
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;
|
|
}
|
|
|
|
// 動態靈敏度 : 準心越靠近敵人,靈敏度越低 (像手把輔瞄的阻尼感)
|
|
// 胸口附近 innerAngle 內維持最低,往畫面邊緣慢慢恢復,到畫面邊緣 = 正常靈敏度 ; 沒偵測到 = 正常
|
|
void DynamicSensitivity(EnemyPerceptor.Detection d)
|
|
{
|
|
float scale = 1f;
|
|
if (d.detected && perceptor.viewCamera != null)
|
|
{
|
|
Camera cam = perceptor.viewCamera;
|
|
float halfScreen = Camera.VerticalToHorizontalFieldOfView(cam.fieldOfView, cam.aspect) * 0.5f;
|
|
float t = Mathf.Clamp01((d.angleError.magnitude - innerAngle) / Mathf.Max(1f, halfScreen - innerAngle));
|
|
|
|
float lowest = Mathf.Lerp(1f, minSensitivityScale, strength);
|
|
scale = Mathf.Lerp(lowest, 1f, t);
|
|
Engaged = true;
|
|
}
|
|
|
|
playerLook.sensitivityScale = scale;
|
|
}
|
|
}
|