Fix manual raycast to loop over Camera.allCameras so it works with camera3

This commit is contained in:
yangbear
2026-07-12 04:50:06 +08:00
parent aceedb44e4
commit 0fa8ba1169
@@ -19,13 +19,11 @@ public class PingClickHandler : MonoBehaviour
var col = GetComponent<Collider>();
if (col == null)
{
// 如果没有碰撞体,为了100%能够被点到,加一个超级巨大的 BoxCollider
var box = gameObject.AddComponent<BoxCollider>();
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,27 +81,31 @@ public class PingClickHandler : MonoBehaviour
PerformClick();
}
// 加入更强力的通用点击检测支持,防止某些射线遮挡 OnMouseDown
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (!_isBlinking) return;
// 简单射线检测,如果打中当前物体,强行触发
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// 遍历所有激活的相机发射射线(解决 camera3 不是主相机导致点不到的问题!)
foreach (var c in Camera.allCameras)
{
if (!c.enabled) continue;
Ray ray = c.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray, 100f);
foreach (var hit in hits)
{
if (hit.collider != null && hit.collider.gameObject == gameObject)
{
Debug.Log($"[PingClick] Update手动射线贯穿点中: {name}");
Debug.Log($"[PingClick] Update手动射线贯穿点中 (使用相机: {c.name}): {name}");
PerformClick();
return;
}
}
}
}
}
public void PerformClick()
{