- Home /
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.
Answer by Kiwasi · Dec 11, 2014 at 06:18 PM
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
Thanks for your reply.
I'll look into this and get back to you. I get the impression that this will work like on the "Survival Shooter" tutorial project. That is, clicking or touching the screen 'shoots' a ray that hits the first object in a line from a given point, (such as the camera origin).
"EventSystem.current.IsPointerOverGameObject" - This only works for the mouse, unfortunately. I am implementing touch screen as well. Touch screen activates "IsPointerOverGameObject" only while the touch is held on the screen, which isn't sufficient.
I tried raycasting. However, the raycasts go through the UI objects, so I had to hunt for a workaround.
Thank you for your support. Question answered.
Answer by static_cast · Dec 08, 2014 at 12:00 AM
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. :P
Thanks for your reply.
The problem with this solution is that it works only if there is just one button the screen, as it takes the 'jump' function away from the character control script. A workaround would be to have a variable changed in another script that holds the jump function.
But!, I still can't find a function or value related to touchscreens - for 'pressed' and 'unpress' on buttons. I tried attaching a script to the button and using "On$$anonymous$$ouseDown", for example, and this didn't work at all, (Debug log used to write a message if successful).
"IsPressed" and "OnPointerDown" both require eventData which the manual describes as "The EventData usually sent by the EventSystem.". Going through the manual to try and find how to provide said EventData manually leads to a HTTP404.
I'm now looking at the "Touch" structure to see whether I can find a solution through that.
I found a low quality workaround that is based on the fact that mouse and touch position begins at 0,0 in the bottom left of the screen. If all GUI elements are anchored to the bottom left of the screen, it is possible to isolate that portion of the screen from triggering in world interaction.
I have noted the poor touchscreen support that Unity provides, should I be considering Unity for future mobile game projects.
Your answer

Follow this Question
Related Questions
How would you divide an object into its faces? 2 Answers
Distribute terrain in zones 3 Answers
Unity Invoke method delay factor vary device to device 1 Answer
Multiple Cars not working 1 Answer
C# Movement Script 3 Answers