修正森林圖無法觸發輔助問題
This commit is contained in:
1 parent
062dd0f9d5
commit
285a4e1e1e
18 files changed
+151
-44
No files matched your search
@@ -3,7 +3,7 @@ using UnityEngine;
|
||||
|
||||
// 模擬 AI 視覺外掛的「眼睛」
|
||||
// 1. 只在螢幕刷新時取樣 (外掛是擷取畫面,畫面一秒只更新刷新率那麼多次)
|
||||
// 2. 敵人被擋住 25% 以上就偵測不到 (視覺辨識的特性)
|
||||
// 2. 敵人被擋住 25% 以上就偵測不到 (視覺辨識的特性) ; 比例依身體各部位的面積加權 : 頭 15%、身體 45%、腿 40%
|
||||
// 3. 結果延遲一段時間才給輔助用 (擷取 → 辨識 → 輸出的鏈路延遲)
|
||||
// 真值 (Truth) 和外掛實際拿到的結果 (Output) 都保留,之後記錄器兩個都存
|
||||
public class EnemyPerceptor : MonoBehaviour
|
||||
@@ -44,13 +44,22 @@ public class EnemyPerceptor : MonoBehaviour
|
||||
private int baseDelayMs;
|
||||
private int jitterMs;
|
||||
private readonly Queue<Detection> pending = new Queue<Detection>();
|
||||
// 各部位佔整個人的比例 (大約是人的外形面積),取樣點的權重照這個分配
|
||||
// 不加權的話,腿有 4 個命中框、佔了大部分取樣點,地上一點小起伏就會讓整個人判定成被擋住
|
||||
private const float HeadShare = 0.15f;
|
||||
private const float BodyShare = 0.45f;
|
||||
private const float LegShare = 0.40f;
|
||||
|
||||
private readonly List<Vector3> samplePoints = new List<Vector3>();
|
||||
private readonly List<BodyPart> sampleParts = new List<BodyPart>();
|
||||
private readonly List<float> sampleWeights = new List<float>();
|
||||
private string lastBlocker = ""; // 除錯用 : 這一幀擋住最多取樣點的物件
|
||||
private readonly RaycastHit[] hitBuffer = new RaycastHit[16];
|
||||
|
||||
void Start()
|
||||
{
|
||||
// prefab 存不住場景物件的參照 : 沒拖的話自動找場景裡的 BotSpawner (新地圖不用再手動接)
|
||||
if (spawner == null) spawner = FindAnyObjectByType<BotSpawner>();
|
||||
// 沒拖、或拖到 prefab 檔案本身 : 自動找場景裡的 BotSpawner (新地圖不用再手動接)
|
||||
spawner = BotSpawner.FindInScene(spawner);
|
||||
|
||||
GameSettings settings = GameSettings.Load();
|
||||
baseDelayMs = settings.assistDelayMs;
|
||||
@@ -92,13 +101,16 @@ public class EnemyPerceptor : MonoBehaviour
|
||||
Hitbox chest = CollectSamplePoints(bot);
|
||||
if (chest == null || samplePoints.Count == 0) return d;
|
||||
|
||||
// 遮擋比例 : 每個取樣點都從相機拉一條線,中間被別的東西擋住或在畫面外就算看不到
|
||||
int visible = 0;
|
||||
foreach (Vector3 p in samplePoints)
|
||||
// 遮擋比例 : 每個取樣點都從相機拉一條線,中間被別的東西擋住或在畫面外就算看不到 (依權重加總)
|
||||
float visible = 0f, total = 0f;
|
||||
blockerCounts.Clear();
|
||||
for (int i = 0; i < samplePoints.Count; i++)
|
||||
{
|
||||
if (IsPointVisible(p, bot.transform)) visible++;
|
||||
total += sampleWeights[i];
|
||||
if (IsPointVisible(samplePoints[i], bot.transform)) visible += sampleWeights[i];
|
||||
}
|
||||
d.visibleRatio = (float)visible / samplePoints.Count;
|
||||
d.visibleRatio = total > 0f ? visible / total : 0f;
|
||||
lastBlocker = MostCommonBlocker();
|
||||
|
||||
// 胸口的角度與螢幕座標
|
||||
Vector3 chestPos = chest.GetComponent<Collider>().bounds.center;
|
||||
@@ -115,35 +127,69 @@ public class EnemyPerceptor : MonoBehaviour
|
||||
}
|
||||
|
||||
// 取樣點 : 每個命中框的中心,身體和腿再加上下兩端 ; 回傳身體 (胸口) 的命中框
|
||||
// 權重 : 每個部位的比例平均分給該部位的所有取樣點 (例如 4 個腿命中框共 12 點,平分 40%)
|
||||
Hitbox CollectSamplePoints(BotHealth bot)
|
||||
{
|
||||
samplePoints.Clear();
|
||||
sampleParts.Clear();
|
||||
sampleWeights.Clear();
|
||||
Hitbox chest = null;
|
||||
int headPoints = 0, bodyPoints = 0, legPoints = 0;
|
||||
|
||||
foreach (Hitbox hitbox in bot.GetComponentsInChildren<Hitbox>())
|
||||
{
|
||||
if (!hitbox.TryGetComponent(out Collider col) || !col.enabled) continue;
|
||||
|
||||
Bounds b = col.bounds;
|
||||
int added = 1;
|
||||
samplePoints.Add(b.center);
|
||||
if (hitbox.part != BodyPart.Head)
|
||||
{
|
||||
samplePoints.Add(b.center + Vector3.up * b.extents.y * 0.7f);
|
||||
samplePoints.Add(b.center - Vector3.up * b.extents.y * 0.7f);
|
||||
added = 3;
|
||||
}
|
||||
|
||||
for (int i = 0; i < added; i++) sampleParts.Add(hitbox.part);
|
||||
if (hitbox.part == BodyPart.Head) headPoints += added;
|
||||
else if (hitbox.part == BodyPart.Body) bodyPoints += added;
|
||||
else legPoints += added;
|
||||
|
||||
if (hitbox.part == BodyPart.Body) chest = hitbox;
|
||||
}
|
||||
|
||||
foreach (BodyPart part in sampleParts)
|
||||
{
|
||||
if (part == BodyPart.Head) sampleWeights.Add(HeadShare / headPoints);
|
||||
else if (part == BodyPart.Body) sampleWeights.Add(BodyShare / bodyPoints);
|
||||
else sampleWeights.Add(LegShare / legPoints);
|
||||
}
|
||||
|
||||
return chest;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, int> blockerCounts = new Dictionary<string, int>();
|
||||
|
||||
string MostCommonBlocker()
|
||||
{
|
||||
string best = ""; int bestCount = 0;
|
||||
foreach (KeyValuePair<string, int> kv in blockerCounts)
|
||||
{
|
||||
if (kv.Value > bestCount) { best = kv.Key; bestCount = kv.Value; }
|
||||
}
|
||||
return bestCount > 0 ? $"{best} ({bestCount} 點)" : "";
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if (!showDebug) return;
|
||||
|
||||
GUI.Label(new Rect(10, 10, 600, 22),
|
||||
$"真值 : {(Truth.detected ? "偵測到" : "沒偵測到")} 可見 {Truth.visibleRatio:P0} | 外掛拿到 : {(Output.detected ? "偵測到" : "沒偵測到")} 延遲 {DelayMs:F0}ms 刷新 {1f / sampleInterval:F0}Hz");
|
||||
if (!string.IsNullOrEmpty(lastBlocker))
|
||||
{
|
||||
GUI.Label(new Rect(10, 30, 600, 22), $"擋住最多的物件 : {lastBlocker}");
|
||||
}
|
||||
|
||||
// 外掛拿到的胸口位置 (GUI 的 y 軸往下,所以要翻過來)
|
||||
if (Output.detected)
|
||||
@@ -168,7 +214,11 @@ public class EnemyPerceptor : MonoBehaviour
|
||||
Transform t = hitBuffer[i].collider.transform;
|
||||
if (t.IsChildOf(bot)) continue; // 機器人自己的移動膠囊
|
||||
if (playerRoot != null && t.IsChildOf(playerRoot)) continue; // 玩家自己
|
||||
return false; // 被牆或障礙物擋住
|
||||
|
||||
// 被牆或障礙物擋住 ; 記下是誰擋的 (除錯用)
|
||||
string name = hitBuffer[i].collider.name;
|
||||
blockerCounts[name] = blockerCounts.TryGetValue(name, out int n) ? n + 1 : 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user