From e33b9d8a02ed29d296bd5cad8a011ef7773fe925 Mon Sep 17 00:00:00 2001 From: yangbear Date: Sun, 12 Jul 2026 04:10:53 +0800 Subject: [PATCH] Revert to OnMouseDown but heavily enlarge colliders to prevent miss clicks --- .../Scripts/Interaction/PingClickHandler.cs | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/Assets/Scripts/Interaction/PingClickHandler.cs b/Assets/Scripts/Interaction/PingClickHandler.cs index 7dd1556..c3c3d14 100644 --- a/Assets/Scripts/Interaction/PingClickHandler.cs +++ b/Assets/Scripts/Interaction/PingClickHandler.cs @@ -19,13 +19,28 @@ public class PingClickHandler : MonoBehaviour var col = GetComponent(); if (col == null) { - col = gameObject.AddComponent(); + col = gameObject.AddComponent(); } + + // 恢复所有类型碰撞体的强制放大逻辑,之前丢掉了 BoxCollider 的处理导致第一步没法点 + float scale = Mathf.Max(Mathf.Abs(transform.lossyScale.x), Mathf.Abs(transform.lossyScale.y), Mathf.Abs(transform.lossyScale.z), 0.001f); + if (col is SphereCollider sphere) { - float scale = Mathf.Max(Mathf.Abs(transform.lossyScale.x), Mathf.Abs(transform.lossyScale.y), Mathf.Abs(transform.lossyScale.z), 0.001f); - sphere.radius = Mathf.Clamp(0.3f / scale, 0.05f, 20f); + sphere.radius = Mathf.Clamp(0.4f / scale, 0.1f, 20f); } + else if (col is BoxCollider box) + { + float s = Mathf.Clamp(0.8f / scale, 0.1f, 40f); + box.size = new Vector3(s, s, s); + } + else if (col is CapsuleCollider cap) + { + cap.radius = Mathf.Clamp(0.4f / scale, 0.1f, 20f); + cap.height = Mathf.Clamp(0.8f / scale, 0.1f, 40f); + } + + col.isTrigger = false; col.enabled = true; CreateOutlineMesh(); @@ -62,36 +77,21 @@ public class PingClickHandler : MonoBehaviour while (true) { if (_outlineMesh != null) _outlineMesh.enabled = !_outlineMesh.enabled; yield return new WaitForSeconds(0.4f); } } - // 弃用 Unity 自带的 OnMouseDown,因为它只能检测最外层第一个碰到的物体,会被前面的隐形物体挡住 - void Update() + // 恢复使用 OnMouseDown,因为 Update 手动射线可能因为某些相机的 LayerMask 配置导致完全扫不到 + void OnMouseDown() { - if (Input.GetMouseButtonDown(0)) - { - if (!_isBlinking) return; - - foreach (var c in Camera.allCameras) - { - if (!c.enabled) continue; - Ray ray = c.ScreenPointToRay(Input.mousePosition); - // 穿透所有物体! - RaycastHit[] hits = Physics.RaycastAll(ray, 1000f); - foreach (var hit in hits) - { - if (hit.collider != null && hit.collider.gameObject == gameObject) - { - Debug.Log($"[PingClick] Update全穿透射线成功点中: {name}"); - PerformClick(); - return; // 已经处理点击,退出 - } - } - } - } + Debug.Log($"[PingClick] OnMouseDown 触发: {name}"); + PerformClick(); } public void PerformClick() { - if (!_isBlinking) return; - + if (!_isBlinking) + { + Debug.Log($"[PingClick] {name} 忽略点击,因为 _isBlinking=false"); + return; + } + if (!_modelShown) { _modelShown = true; @@ -233,13 +233,20 @@ public class PingClickHandler : MonoBehaviour var col = GetComponent(); if (col == null) { - col = gameObject.AddComponent(); + col = gameObject.AddComponent(); } + + float scale = Mathf.Max(Mathf.Abs(transform.lossyScale.x), Mathf.Abs(transform.lossyScale.y), Mathf.Abs(transform.lossyScale.z), 0.001f); if (col is SphereCollider sphere) { - float scale = Mathf.Max(Mathf.Abs(transform.lossyScale.x), Mathf.Abs(transform.lossyScale.y), Mathf.Abs(transform.lossyScale.z), 0.001f); - sphere.radius = Mathf.Clamp(0.3f / scale, 0.05f, 20f); + sphere.radius = Mathf.Clamp(0.4f / scale, 0.1f, 20f); } + else if (col is BoxCollider box) + { + float s = Mathf.Clamp(0.8f / scale, 0.1f, 40f); + box.size = new Vector3(s, s, s); + } + col.enabled = true; _isBlinking = true;