How to prevent world interaction when using UI elements?

Hello,

I’m making a mobile game. During the game, on the screen there are some UI buttons. If the player presses on a button, the button is pressed and an event occurs.

If the player presses on the screen, the player’s character jumps.

The problem is - when pressing a UI button, the character jumps as well - because screen touches must be taken as an input for this purpose.

How can I prevent the player’s character from jumping, if the touch screen press is made on a button, and not on any other part of the screen? I saw a function “.IsPressed” that the “Button” component inherits. However, this is a protected function for triggering an event…

I need some way to either block out part of the screen from causing the character to jump or some way of preventing the jump action from triggering when pressing on a UI button. But there doesn’t seem to be any way of getting the state of a button.

I’d be grateful for any help.

Thanks.

You could check this…

bool mybuttonisbeingpressed

if(!mybuttonisbeingpressed)
 jump


void onpress()
{ mybuttonisbeingpressed = true }

void offpress()
{ mybuttonisbeingpressed = false }

This is psuedo-code, as I haven’t gotten the chance to program much with the 4.6 UI, but it’s a solution that works. :stuck_out_tongue:

The best way to do this is to convert all your world clicks into events, using the Unity event system. This means the event system will automatically prioritise the clicks based on the distance from the camera. You will need to add a physics raycaster to your camera, and implement the various event interfaces on your scripts.

As an alternative you can check EventSystem.current.IsPointerOverGameObject