Пример #1
0
    private void RayCastMouse()
    {
        Vector3 mousePos = Input.mousePosition;

        mousePos.z = 10;

        Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);

        RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero, Mathf.Infinity, layerMask);

        //RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero, Mathf.Infinity);

        if (hit)
        {
            if (hit.collider.tag == "UI")   // Return if UI Canvas is hit
            {
                Debug.Log("Ray hit UI");
                return;
            }

            if (hit.collider.GetComponent <Selectable>())
            {
                Selectable target = hit.collider.GetComponent <Selectable>();
                if (target.GetWaitForClicks() > 0)
                {
                    target.Clicked();
                    return;
                }
                if (!target.IsSelectable())
                {
                    Debug.Log("Target not selectable");
                    return;  // If object is currently not selectable, then return.
                }
                SelectObject(hit.collider.GetComponent <Selectable>());
            }

            else if (selectedObject != null)
            {
                UnselectObject();
            }
        }

        else
        {
            UnselectObject();
        }
    }