Fix ping scaling issues and disable touchArray raycast block
This commit is contained in:
@@ -16,26 +16,26 @@ public class PingClickHandler : MonoBehaviour
|
||||
if (_modelRenderer == null) _modelRenderer = GetComponentInChildren<MeshRenderer>();
|
||||
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
||||
|
||||
// 修复之前粗暴销毁导致的编辑器报错,改为安全地复用或添加 Collider
|
||||
var col = GetComponent<Collider>();
|
||||
if (col == null)
|
||||
{
|
||||
col = gameObject.AddComponent<SphereCollider>();
|
||||
}
|
||||
|
||||
// 强行把碰撞体的世界大小固定在一个比较合适的尺寸(比如世界尺寸 0.15)
|
||||
// 这样无论模型怎么缩放,点击区域都是固定的
|
||||
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 maxDim = 0.5f;
|
||||
if (_modelRenderer != null)
|
||||
maxDim = Mathf.Max(_modelRenderer.bounds.size.x, Mathf.Max(_modelRenderer.bounds.size.y, _modelRenderer.bounds.size.z));
|
||||
sphere.radius = Mathf.Max(maxDim * 0.6f, 0.5f);
|
||||
sphere.radius = 0.15f / scale;
|
||||
}
|
||||
else if (col is BoxCollider box)
|
||||
{
|
||||
box.size = new Vector3(Mathf.Max(box.size.x, 0.8f), Mathf.Max(box.size.y, 0.8f), Mathf.Max(box.size.z, 0.8f));
|
||||
float s = 0.3f / scale;
|
||||
box.size = new Vector3(s, s, s);
|
||||
}
|
||||
|
||||
// 强制碰撞体处于启用状态
|
||||
col.enabled = true;
|
||||
|
||||
CreateOutlineMesh();
|
||||
@@ -74,18 +74,29 @@ public class PingClickHandler : MonoBehaviour
|
||||
|
||||
void OnMouseDown()
|
||||
{
|
||||
Debug.Log($"[PingClick] 接收到鼠标点击: {name}");
|
||||
PerformClick();
|
||||
}
|
||||
|
||||
public void PerformClick()
|
||||
{
|
||||
if (!_isBlinking) return; // 防止重复点击
|
||||
if (!_isBlinking)
|
||||
{
|
||||
Debug.Log($"[PingClick] {name} 忽略点击,因为 _isBlinking=false");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var c in Object.FindObjectsOfType<Camera>())
|
||||
{
|
||||
if (!c.enabled) continue;
|
||||
foreach (var h in Physics.RaycastAll(c.ScreenPointToRay(Input.mousePosition)))
|
||||
if (h.collider?.transform.parent?.name == "touchArray") return;
|
||||
{
|
||||
if (h.collider?.transform.parent?.name == "touchArray")
|
||||
{
|
||||
Debug.Log($"[PingClick] {name} 的点击被 touchArray 的子物体 {h.collider.name} 拦截,强制忽略拦截!");
|
||||
// 之前这里是 return; 导致拦截。现在我们仅仅打印日志,不 return,强行穿透!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!_modelShown)
|
||||
@@ -230,7 +241,11 @@ public class PingClickHandler : MonoBehaviour
|
||||
if (col == null)
|
||||
{
|
||||
col = gameObject.AddComponent<SphereCollider>();
|
||||
((SphereCollider)col).radius = 0.5f;
|
||||
}
|
||||
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 = 0.15f / scale;
|
||||
}
|
||||
col.enabled = true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user