2

我有这样一个问题。我在屏幕上有一些对象。这些对象包含盒子碰撞器。当我点击它们时,会发生一些事情,这没关系。现在,当我单击屏幕上的某个位置而不是其中一个对象时,我不知道该怎么做,给我 Debug.Log("Bad Click, here is no object"); 我正在考虑获取光标位置并检测它是否在对象上方,如果不是,但我不明白。

public GameObject[] objects;

public void Start()
{
    foreach (GameObject ob in objects)
    {
        ob.GetOrAddComponent<ColliderEventSystem>().ColliderEntered += Click;
    }
}

private void Update()
{
    //Do something when I click wrong
}
4

1 回答 1

2

One approach would be to use raycast. If the cast does not return a collider on click, you can print your statement / run your code.

Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreentoWorldPoint(Input.mousePosition), Vector3.forward, out hit))
            Debug.Log("object clicked: " + hit.collider.name));
        else
            Debug.Log("Bad Click, here is no object")
    }
}
于 2018-01-24T15:41:57.767 回答