From fb2dbfca9931cabb804ffa6c28c30c84d597282e Mon Sep 17 00:00:00 2001 From: keshaohong <740612340@qq.com> Date: Sun, 28 Jun 2026 23:23:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A7=86=E8=A7=89=E8=AE=AD?= =?UTF-8?q?=E7=BB=83=E8=B7=9D=E7=A6=BB=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unity/Assets/2.5D Engine/Enemy.prefab | 1 + unity/Assets/enemy/EnemyAI.cs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/unity/Assets/2.5D Engine/Enemy.prefab b/unity/Assets/2.5D Engine/Enemy.prefab index 529b712..4948ea1 100644 --- a/unity/Assets/2.5D Engine/Enemy.prefab +++ b/unity/Assets/2.5D Engine/Enemy.prefab @@ -219,6 +219,7 @@ MonoBehaviour: serializedVersion: 2 m_Bits: 0 rayOffsetY: 0.5 + maxSightDistance: 4 loseSightTime: 3 listenRange: 15 bellArriveDistance: 1 diff --git a/unity/Assets/enemy/EnemyAI.cs b/unity/Assets/enemy/EnemyAI.cs index eabedff..55ad937 100644 --- a/unity/Assets/enemy/EnemyAI.cs +++ b/unity/Assets/enemy/EnemyAI.cs @@ -35,6 +35,9 @@ namespace IndianOceanAssets.Engine2_5D [Tooltip("射线起点偏移(避免从地面发射)")] [SerializeField] private float rayOffsetY = 0.5f; + [Tooltip("最大视野距离(超过此距离看不到玩家)")] + [SerializeField] private float maxSightDistance = 15f; + [Header("脱离计时")] [Tooltip("玩家脱离视线后,继续追击的时间(秒)")] [SerializeField] private float loseSightTime = 3f; @@ -255,18 +258,24 @@ namespace IndianOceanAssets.Engine2_5D } /// - /// 检测是否能看到玩家(射线检测) + /// 检测是否能看到玩家(射线检测 + 距离检测) /// private bool CanSeePlayer() { if (playerTarget == null) return false; - // 射线起点(稍微抬高,避免从地面发射) + // 计算距离 Vector3 rayStart = transform.position + Vector3.up * rayOffsetY; Vector3 rayEnd = playerTarget.position + Vector3.up * rayOffsetY; Vector3 dir = rayEnd - rayStart; float dist = dir.magnitude; + // 距离检测:超过最大视野距离看不到玩家 + if (dist > maxSightDistance) + { + return false; + } + // 调试:在Scene视图中绘制射线 Debug.DrawLine(rayStart, rayEnd, CanSeePlayerDebugColor()); @@ -351,6 +360,10 @@ namespace IndianOceanAssets.Engine2_5D Gizmos.color = stateColor; Gizmos.DrawWireSphere(transform.position, 0.5f); + // 绘制最大视野距离(绿色线框) + Gizmos.color = new Color(0f, 1f, 0f, 0.2f); + Gizmos.DrawWireSphere(transform.position, maxSightDistance); + // 绘制聆听范围 Gizmos.color = new Color(0f, 0.85f, 1f, 0.3f); Gizmos.DrawWireSphere(transform.position, listenRange);