diff --git a/Assets/PathBerserker2d/Scripts/PathBerserker2d/NavAgent/NavAgent.cs b/Assets/PathBerserker2d/Scripts/PathBerserker2d/NavAgent/NavAgent.cs index 9c3e6b09..aa90f606 100644 --- a/Assets/PathBerserker2d/Scripts/PathBerserker2d/NavAgent/NavAgent.cs +++ b/Assets/PathBerserker2d/Scripts/PathBerserker2d/NavAgent/NavAgent.cs @@ -620,24 +620,27 @@ namespace PathBerserker2d return PathTo(goal); } - // [项目新增] 目标点吸附:把任意世界点映射到最近的可行走段,再按 endMargin 从段两端内缩, - // 得到"身体真正站得住"的世界点(避免落在角色宽度站不住的崖沿被移动层夹停)。 - // 与 SetRandomDestinationOnCurrentSegment 同源:endMargin 传 agent 半宽 + 落崖检测余量。段短于 - // 2*endMargin → 退化到中点。映射方式与 PB2d 自身 UpdatePath 映射目标一致(见 UpdatePath): - // 搜索距离 = agent 到目标的距离(+余量)、用不带 agent 过滤的 TryMapPoint(就近段几何映射,可达性交给 - // allowCloseEnoughPath 的寻路阶段),故只要图上有段就一定能吸附——不会像固定小半径那样漏掉稍离地的点。 + // [项目新增] 目标点吸附:把任意世界点映射到最近的可行走段(与 PB2d 自身"把鼠标点击等任意点映射到 + // 最近导航位置"同一机制),再按 endMargin 从段两端内缩,得到"身体真正站得住"的世界点(避免落在角色宽度 + // 站不住的崖沿被移动层夹停)。与 SetRandomDestinationOnCurrentSegment 同源:endMargin 传 agent 半宽+ + // 落崖检测余量。段短于 2*endMargin → 退化到中点。 + // + // maxSnapDistance = 允许"点离最近段"的最大真实距离(m),语义同 PathBerserker2dSettings.closestToSegmentMaxDistance + // ("想多大就多大")。注意 TryMapPoint 传入的是 AABB 边长、可靠搜索半径只有其一半(见 TryMapPointSurface 的 + // queryAABB=pointMapDistance/2),故这里传 2*maxSnapDistance 以保证"点在 maxSnapDistance 内一定被吸附"。 + // 只要目标离某条烘焙段不超过 maxSnapDistance,就一定映射得到——不会因固定小半径漏掉离地稍远的点。 /// - /// Maps to the NEAREST walkable segment (same robustness as - /// PB2d's own goal mapping in UpdatePath — search distance scales with agent→target distance), - /// then insets by from BOTH segment ends so the returned point is - /// one a bodied agent can actually stand on (not clamped at a ledge). A segment shorter than - /// 2*endMargin degenerates to its midpoint. Returns false only if the graph has no segment at all. + /// Maps to the NEAREST walkable segment within + /// (same mechanism PB2d uses to map an arbitrary point such as a + /// mouse click to the graph — cf. closestToSegmentMaxDistance), then insets by + /// from BOTH segment ends so the returned point is one a bodied agent can + /// actually stand on (not clamped at a ledge). A segment shorter than 2*endMargin degenerates to its + /// midpoint. Returns false only if no baked segment lies within maxSnapDistance of the target. /// - public bool TryGetStandablePointNear(Vector2 worldTarget, float endMargin, out Vector2 standable) + public bool TryGetStandablePointNear(Vector2 worldTarget, float maxSnapDistance, float endMargin, out Vector2 standable) { - // 与 UpdatePath 映射目标同款:半径随距离放大,保证图上有段就映射得到最近点。 - float searchDist = Vector2.Distance(Position, worldTarget) + 0.1f; - if (!PBWorld.TryMapPoint(worldTarget, searchDist, out var ptr)) + // TryMapPoint 的可靠搜索半径 ≈ 传入值/2(broadphase AABB 半边=pointMapDistance/2),故传 2 倍补偿。 + if (!PBWorld.TryMapPoint(worldTarget, maxSnapDistance * 2f, out var ptr)) { standable = worldTarget; return false; diff --git a/Assets/_Game/Scripts/Enemies/Navigation/EnemyNavAgent.cs b/Assets/_Game/Scripts/Enemies/Navigation/EnemyNavAgent.cs index d8ea7eff..daf66248 100644 --- a/Assets/_Game/Scripts/Enemies/Navigation/EnemyNavAgent.cs +++ b/Assets/_Game/Scripts/Enemies/Navigation/EnemyNavAgent.cs @@ -35,6 +35,12 @@ namespace BaseGames.Enemies.Navigation [Tooltip("两次重算最小间隔(s):留出跨帧路径计算完成的时间")] [SerializeField] private float _repathMinInterval = 0.25f; + [Header("目标点吸附")] + [Tooltip("把巡逻/移动目标吸附到最近可行走段时,允许目标离最近段的最大真实距离(m)。语义同 PB2d 的 " + + "closestToSegmentMaxDistance——想多大就多大;只要目标在此距离内一定吸附得到。仅当目标离所有" + + "烘焙段都超过此值(真正脱离导航面)才判失败。")] + [SerializeField] private float _maxSnapDistance = 10f; + // 重寻路防抖状态 private Vector2 _lastPathGoal; private bool _hasPathGoal; @@ -178,12 +184,12 @@ namespace BaseGames.Enemies.Navigation // 目标点吸附:把任意世界目标映射到最近可行走段、并按身体宽度(EdgeSafeMargin)从段两端内缩, // 得到"身体真正站得住"的点。身体宽度是本执行层独占的知识——意图层(AI/Locomotion)只递原始目标。 - // 底层用与 PB2d 自身目标映射同款的距离自适应搜索,只要图上有段就一定吸附得到;返回 false 仅表示图为空。 + // 用 PB2d 映射任意点到导航面的机制(_maxSnapDistance 内一定命中最近段);返回 false 仅表示目标真正脱离导航面。 public bool ResolveStandablePoint(Vector2 target, out Vector2 standable) { if (_navAgent == null) { standable = target; return false; } float margin = _enemyMovement != null ? _enemyMovement.EdgeSafeMargin : 0f; - return _navAgent.TryGetStandablePointNear(target, margin, out standable); + return _navAgent.TryGetStandablePointNear(target, _maxSnapDistance, margin, out standable); } public bool IsNearEdge()