From 0fa8ba11692d2c346cba6db9a7dcf63e29506b64 Mon Sep 17 00:00:00 2001 From: yangbear Date: Sun, 12 Jul 2026 04:50:06 +0800 Subject: [PATCH] Fix manual raycast to loop over Camera.allCameras so it works with camera3 --- .../Scripts/Interaction/PingClickHandler.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/Interaction/PingClickHandler.cs b/Assets/Scripts/Interaction/PingClickHandler.cs index fd37ccd..8695063 100644 --- a/Assets/Scripts/Interaction/PingClickHandler.cs +++ b/Assets/Scripts/Interaction/PingClickHandler.cs @@ -19,13 +19,11 @@ public class PingClickHandler : MonoBehaviour var col = GetComponent(); if (col == null) { - // 如果没有碰撞体,为了100%能够被点到,加一个超级巨大的 BoxCollider var box = gameObject.AddComponent(); - box.size = new Vector3(2f, 2f, 2f); // 固定绝对大尺寸 + box.size = new Vector3(2f, 2f, 2f); col = box; } - // 不管是什么碰撞体,强制设置为绝对足够大的物理尺寸 if (col is SphereCollider sphere) { sphere.radius = 1.0f; @@ -48,8 +46,6 @@ public class PingClickHandler : MonoBehaviour _isBlinking = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); - - Debug.Log($"[PingClick] {name} OnEnable 完成,碰撞体已强制放大。"); } void CreateOutlineMesh() @@ -85,23 +81,27 @@ public class PingClickHandler : MonoBehaviour PerformClick(); } - // 加入更强力的通用点击检测支持,防止某些射线遮挡 OnMouseDown void Update() { if (Input.GetMouseButtonDown(0)) { if (!_isBlinking) return; - // 简单射线检测,如果打中当前物体,强行触发 - Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); - RaycastHit[] hits = Physics.RaycastAll(ray, 100f); - foreach (var hit in hits) + // 遍历所有激活的相机发射射线(解决 camera3 不是主相机导致点不到的问题!) + foreach (var c in Camera.allCameras) { - if (hit.collider != null && hit.collider.gameObject == gameObject) + if (!c.enabled) continue; + + Ray ray = c.ScreenPointToRay(Input.mousePosition); + RaycastHit[] hits = Physics.RaycastAll(ray, 100f); + foreach (var hit in hits) { - Debug.Log($"[PingClick] Update手动射线贯穿点中: {name}"); - PerformClick(); - return; + if (hit.collider != null && hit.collider.gameObject == gameObject) + { + Debug.Log($"[PingClick] Update手动射线贯穿点中 (使用相机: {c.name}): {name}"); + PerformClick(); + return; + } } } }