Revert to OnMouseDown but heavily enlarge colliders to prevent miss clicks

This commit is contained in:
yangbear
2026-07-12 04:10:53 +08:00
parent 51fe94b3e1
commit e33b9d8a02
+38 -31
View File
@@ -19,13 +19,28 @@ public class PingClickHandler : MonoBehaviour
var col = GetComponent<Collider>(); var col = GetComponent<Collider>();
if (col == null) if (col == null)
{ {
col = gameObject.AddComponent<SphereCollider>(); col = gameObject.AddComponent<BoxCollider>();
} }
// 恢复所有类型碰撞体的强制放大逻辑,之前丢掉了 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) 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.4f / scale, 0.1f, 20f);
sphere.radius = Mathf.Clamp(0.3f / scale, 0.05f, 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; col.enabled = true;
CreateOutlineMesh(); CreateOutlineMesh();
@@ -62,36 +77,21 @@ public class PingClickHandler : MonoBehaviour
while (true) { if (_outlineMesh != null) _outlineMesh.enabled = !_outlineMesh.enabled; yield return new WaitForSeconds(0.4f); } while (true) { if (_outlineMesh != null) _outlineMesh.enabled = !_outlineMesh.enabled; yield return new WaitForSeconds(0.4f); }
} }
// 弃用 Unity 自带的 OnMouseDown,因为它只能检测最外层第一个碰到的物体,会被前面的隐形物体挡住 // 恢复使用 OnMouseDown,因为 Update 手动射线可能因为某些相机的 LayerMask 配置导致完全扫不到
void Update() void OnMouseDown()
{ {
if (Input.GetMouseButtonDown(0)) Debug.Log($"[PingClick] OnMouseDown 触发: {name}");
{ PerformClick();
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; // 已经处理点击,退出
}
}
}
}
} }
public void PerformClick() public void PerformClick()
{ {
if (!_isBlinking) return; if (!_isBlinking)
{
Debug.Log($"[PingClick] {name} 忽略点击,因为 _isBlinking=false");
return;
}
if (!_modelShown) if (!_modelShown)
{ {
_modelShown = true; _modelShown = true;
@@ -233,13 +233,20 @@ public class PingClickHandler : MonoBehaviour
var col = GetComponent<Collider>(); var col = GetComponent<Collider>();
if (col == null) if (col == null)
{ {
col = gameObject.AddComponent<SphereCollider>(); 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) 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.4f / scale, 0.1f, 20f);
sphere.radius = Mathf.Clamp(0.3f / scale, 0.05f, 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; col.enabled = true;
_isBlinking = true; _isBlinking = true;