fix: Shose用Update+Physics.Raycast替代OnMouseDown处理点击
This commit is contained in:
@@ -5,21 +5,20 @@ public class ShoseClickHandler : MonoBehaviour
|
||||
public GameObject nextObject;
|
||||
public Transform targetPoint;
|
||||
private MeshRenderer _outlineMesh;
|
||||
private bool _clicked;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
// 强制确保有足够大的 collider
|
||||
var col = GetComponent<Collider>();
|
||||
if (col == null) col = gameObject.AddComponent<SphereCollider>();
|
||||
if (col is SphereCollider sc) { sc.radius = 1.2f; sc.isTrigger = false; }
|
||||
// 干掉可能拦截点击的旧组件
|
||||
var drag = GetComponent<DraggableObject>();
|
||||
if (drag != null) Destroy(drag);
|
||||
|
||||
CreateOutlineMesh();
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(BlinkOutline());
|
||||
Debug.Log($"[ShoseClick] OnEnable outline={_outlineMesh!=null} col radius={(col is SphereCollider sc2?sc2.radius:-1)}");
|
||||
_clicked = false;
|
||||
Debug.Log($"[ShoseClick] OnEnable outline={_outlineMesh!=null} col={(col is SphereCollider sc2?sc2.radius:-1)}");
|
||||
}
|
||||
|
||||
void CreateOutlineMesh()
|
||||
@@ -52,13 +51,29 @@ public class ShoseClickHandler : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseDown()
|
||||
void Update()
|
||||
{
|
||||
if (_clicked || !Input.GetMouseButtonDown(0)) return;
|
||||
foreach (var cam in Camera.allCameras)
|
||||
{
|
||||
if (!cam.enabled) continue;
|
||||
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f) && hit.collider != null && hit.collider.gameObject == gameObject)
|
||||
{
|
||||
OnClicked();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnClicked()
|
||||
{
|
||||
_clicked = true;
|
||||
Debug.Log("[ShoseClick] ★ 被点击了 ★");
|
||||
StopAllCoroutines();
|
||||
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
||||
if (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; Debug.Log($"[ShoseClick] 移动到 {targetPoint.name}"); }
|
||||
else Debug.LogWarning("[ShoseClick] targetPoint 为空!");
|
||||
else Debug.LogWarning("[ShoseClick] targetPoint 为空");
|
||||
if (nextObject != null) nextObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user