0

I need to when the user touches in any point of the screen the camera recieves pointer down|up events. I tried to attach BoxCollider2D to camera with script:

public class PlayerController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public void OnPointerUp(PointerEventData eventData)
    {
        // Some logic
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        // Some logic
    }
}

but it doesn't work.

How to make it?

P.S. I don't care if it's click on GameObject or not, it doesn't matter, the most important is that the user clicked screen. BUT I need to ignore clicks on UI elements.
P.P.S I use Unity3D 2018.3.12f1.

Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 1
    You can use Input.GetButtonDown to achieve this but it won't ignore clicking on UI element. What you can do instead is to create a UI label of the same size of your screen and detect click on it. If you put this label below all other UI element it will do it. – Pierre Baret Apr 15 '19 at 14:55
  • 1
    Attach Physic2DRaycaster Component to the Camera first! – Qusai Azzam Apr 15 '19 at 15:38

1 Answers1

0

In short I am not sure if it's optimal solution or not but using some other topics and Pierre Baret's advice I came up with this script:

[RequireComponent(typeof(CatController))]
public class PlayerController : MonoBehaviour
{
    public float delay = 0.25f;

    private CatController _cat;
    private short _clicked = 0;
    private float _pressingTime = 0f;
    private bool _isPressed = false;

    private void Awake()
    {
        _cat = GetComponent<CatController>();
    }

    private void Update()
    {
        if (_pressingTime > 0)
        {
            _pressingTime -= Time.deltaTime;
        }

        if (Input.GetMouseButtonDown(0))
        {
            if (!IsOverUI())
            {
                OnPointerPressedDown();
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            _isPressed = false;
            OnPointerPressedUp();
        }
        else if (_isPressed && _pressingTime <= 0)
        {
            OnPointerPressing();
        }
    }

    private void OnPointerPressedDown()
    {
        _isPressed = true;

        if (_clicked == 1)
        {
            if (_pressingTime > 0f)
            {
                OnDoubleClick();
                _clicked = 0;
            }
            else
            {
                _pressingTime = delay;
            }
        }
        else
        {
            _clicked = 1;
            _pressingTime = delay;
        }
    }

    private void OnPointerPressedUp()
    {
        _cat.Stop();
    }

    private void OnDoubleClick()
    {
        _cat.Jump();
    }

    private void OnPointerPressing()
    {
        var clickPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        var direction = clickPos.x < 0.5 ? CatController.Direction.Left : CatController.Direction.Right;
        _cat.Walk(direction);
    }

    private static bool IsOverUI()
    {
        var pointerData = new PointerEventData(EventSystem.current)
        {
            position = Input.mousePosition
        };
        var results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(pointerData, results);

        if (results.Count > 0)
        {
            for (int i = 0; i < results.Count; i++)
            {
                if (results[i].gameObject.GetComponent<CanvasRenderer>())
                {
                    return true;
                }
            }
        }

        return false;
    }
}

Maybe someone will look for same controller.

Denis Sologub
  • 7,277
  • 11
  • 56
  • 123