I am trying to implement a joystick in my 2d game. However I don’t understand how the OnDrag event works. The event seems only to get called when I drag on the UI element containing the script, but if I start dragging on the UI element and then move to a position not on the UI element it also gets called. Could anyone explain to me how the OnDrag event works and when it gets called?
Hi @Tibor_coder ,
Maybe the issue is that the Render mode of the Canvas is not set to Screen Space - Overlay.
You can have more info about it on this thread: [SOLVED] Having troubles with OnDrag
I hope this helps!
The problem is that when I start dragging somewhere that is not on the joystick it doesn’t get called. But if I drag in the joystick it gets called. Even weirder if i start dragging from the joystick and move to somewhere not on the joystick it gets called.
“The problem is that when I start dragging somewhere that is not on the joystick it doesn’t get called.”
…
“Even weirder if i start dragging from the joystick and move to somewhere not on the joystick it gets called.”
I think your issue is with the expected behavior.
For example if you implement IBeginDragHandler, IDragHandler and IEndDragHandler you’ll notice that begin, drag and end drag actually get called on UI element you started dragging over, you are not going to get drag if you press mouse button then drag your mouse cursor inside your button - AFAIK.
Edit:
Your OP doesn’t actually explain what you want to do; a button you can drag and also tap, or a button that operates like old arcade game button, where you can start a button press outside a button and then slide your finger over the button to register a press… so could you be more specific about what you are trying to achieve?