- Home /
Detect if player wants to pause or to jump
hi i'm making a mobiel game and every time the player taps the screen the player jump.that's cool but i dont want him to jump when the player presses the pause button.i searched for a solution and didn't find one. ps:i cant check if the player presses a UI elemnt because of the background:if covers the whole screen. if there anyway i can check if player presses a button or i can check the tag or the layer of the UI element.
Answer by goutham12 · Dec 03, 2019 at 03:09 PM
there is two approaches i can give you.
1) as mentioned above create a invisible button that behind pause menu. the reason for the delay on button click is the function will trigger when you left the fingure. so remove button component for that button object, and an event trigger i.e onpointer down. for more info
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.EventTrigger.html
in all you need only on pointer down only.
2) the simplest way is restricting touch to be pressed there.
i mean
void Update(){
if (Input.GetMouseButtonDown(0))
{
var mousePos =
Camera.main.ScreenToViewportPoint(Input.mousePosition);
// assume your pause button is right top corner
if(mousePos.x < 0.8 && mousePos.y > 0.2){
//jump();
}
}
}
thanks,i tried the second solution and it works fine
Answer by developeration1 · Dec 02, 2019 at 10:03 PM
You can try by checking if the player is tapping and the tap position is inside a limit position in x and y. Im considering that you have your UI buttons in a corner, if that's so, it may work.
Answer by Anis1808 · Dec 02, 2019 at 11:03 PM
What I did for a similar game was adding an invisible button (which will act as your main screen) BEHIND the pause button and which when clicked, does the jump action so you would need to change your scripts a bit but it surely works.
i tried it but there is a little delay between the tap and the jump do you have any solution?
The only other way I see is to detect the click location and not accept the top corner (if your button is in screen percentage it's easier, otherwise you have to calculate the button's size against the screen size programatically)