2

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:

image

halfer
  • 19,824
  • 17
  • 99
  • 186
agiro
  • 2,018
  • 2
  • 30
  • 62
  • You asked or someone else asked this exact question about few days ago and it was flagged as a dup. I will point it out to you again, there is no `PointerDown` and `PointerUp`. There are `OnPointerDown` and `OnPointerUp`. Fix that and try again, your code should work. – Programmer Aug 29 '17 at 17:53
  • I didn't explain well enough, but I hooked up those two methods to the `OnPointerUp` and down methods available in the `EventTrigger`. This code I wrote works, but is fragile and I'm looking for a polling based way to do the same. – agiro Aug 29 '17 at 17:56
  • Could you explain why the EvenTriggers OnPointerUp/Down methods are not reliable enough for you? Maybe there is just little mistake in your code which make them look unreliable – Tobias Theel Aug 29 '17 at 18:04
  • Sure, editing the question... – agiro Aug 29 '17 at 18:07
  • You still haven't posted code that shows how you hooked this up to EventTrigger. – Programmer Aug 29 '17 at 18:14
  • Dragged in the editor, I add a screenshot. – agiro Aug 29 '17 at 18:15
  • _Unrelated:_ I read back I write "editing the question..." with the 3 dots at the end. After reading it back again it sounds like I have problems with correcting my mistakes in the question :D The 3 dots meant "work in progress, stand by...". Thought I write just in case. – agiro Aug 29 '17 at 18:20
  • 2
    `EventTrigger` captures **all** the events which is slow. A proper way to do this without doing that is to implement `IPointerDownHandler` and `IPointerUpHandler` from your script then use the `OnPointerDown` and `OnPointerUp` functions. See [this](https://stackoverflow.com/a/41392130/3785314) for an example. It should be attached to each Object then have a public non static boolean variable you can use on them. – Programmer Aug 29 '17 at 18:21
  • Now that's what I call an answer back there. I go check that out. By the way, what's with the `Input.GetTouch`? You said in the answer "You don't use with the new UI", but why? – agiro Aug 29 '17 at 18:23
  • @Programmer this sounds like a valid answer and should be transformed into one :) – Tobias Theel Aug 29 '17 at 18:23
  • That's fine. I consider it a duplicate but was trying to see if that's what OP is already doing before closing it. @agiro Did that solve your problem? It should because there is no known slow bug with the EventSystem – Programmer Aug 29 '17 at 18:26
  • 1
    You gave me a lot to read and to implement :D checking it now. – agiro Aug 29 '17 at 18:28
  • 1
    Yep, that sounds like the way to go. You know Unity like you are the lead architect of it :D – agiro Aug 29 '17 at 18:31
  • by the way, @TobiasTheel You did a good job too. Thanks for your efforts, I hit an upvote. – agiro Aug 29 '17 at 18:32

1 Answers1

2

You are missing the parameters and you have the wrong naming. Have a look at the unity scripting reference for further information.

public static bool JUMP;
public void OnPointerDown(PointerEventData eventData){
    JUMP = true;
}

public void OnPointerUp(PointerEventData eventData){
}

Unity Scripting Reference: https://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerUp.html

Update If you dont want to use OnPointerUp/Down Method. Another Solution would be to use

Input.GetTouch

These touch events hold "phases" telling you if the touch just began, or is released etc. So you can use the touch position of this event and check if it overlaps your UI element. If it is overlapping and is being pressed, well than you want to jump up. If it is released you want to stop jumping up. You could check that in an update method of a script that you attach to your ui component.

https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

Update2 U can use RectTransformUtility.ScreenPointToLocalPointInRectangle to find out, if your touch event hits your UI Element.

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45