I'm aware of the EventTrigger
utility in Unity and currently use it to check if the screen is "being pressed":
public static bool JUMP;
public void PointerDown()
{
JUMP = true;
}
public void PointerUp()
{
JUMP = false;
}
However the issue with this is that on level load the trigger isn't notified of the OnPointerUp
and the player is stuck in jumping.
As a result I'm looking for a solution that basically returns true when the UI element is being pressed and otherwise returns false, probably using polling
. So basically GetMouseButton
just for UI elements and on a touch screen.
I looked over the EventTrigger
system but didn't find what I'm after.
Edit
The two methods I presented are hooked up to the gameobject
's EventTrigger
's OnPointerUp
and OnPointerDown
, respectively. The code I wrote here actually works, but I need a more reliable solution, one that probably uses polling (e.g. called from a FixedUpdate()
to get updated).
Why is this not reliable enough for me, or for anyone else
The engine has to be notified every time the pointer is up in order not to get stuck - in my case - in jumping. Now, there are some cases in mobile games where this just doesn't happen:
- User accidentally touching the screen while holding the device
- Lagging/stuttering, maybe not due to the app itself, but any other thing going on on the device
- Level load, ad loading
I messed with the app for testing purposes and found if used by this way I presented it actually isn't hard to get the character stuck in - in my case - jumping.
Here's how the code is connected to the EventTrigger
: