- Home /
Detect tap only if button was not pressed
This has most probably been asked heaps of times, but I'm not quite sure what to search.. A link to a thread would do just as fine as an answer.
So I have an iOS sorta game in development where you can tap to jump, but also can click on buttons to like pause, for example. When I click a button, it also jumps - and limiting the area which can be clicked on to jump isn't an ideal solution - so is there some way to check if a button was pressed on the tap - otherwise jump?
EDIT - Right now, jump is done by this:
void Update ()
{
if ((Input.GetMouseButtonDown(0) || Input.GetKeyDown("space")) && grounded)
{
GetComponent<Rigidbody>().AddForce(new Vector3(0f, 250f, 0f));
grounded = false;
}
}
void OnCollisionStay (Collision other)
{
if (other.gameObject.tag == "Block")
{
grounded = true;
}
}
void OnCollisionExit (Collision other)
{
if (other.gameObject.tag == "Block")
{
grounded = false;
}
}
(May have missed some syntax while I copied it)
Write your code for jump so that we can easily modify your code to check button clicked or not .
I tried adding "grounded = false" under the pause button, but of course that wouldn't work since thats when the button is released, not pressed.
This solves the problem for now, checking that the mouse ISNT over the button when it sees there was a mousedown event (Beward: It's also kinda messy I could probably remove a few brackets).
if(Input.Get$$anonymous$$ouseButtonDown(0) && !(Input.mousePosition.x > Screen.width - (20 + (50 guiSize)) && (Input.mousePosition.y > Screen.height - (20 + (50 guiSize)))))
Still, an actual solution would be appreciated. (Also I haven't tested this on iOS, it may actually not help)
Answer by pedrolb · Oct 30, 2017 at 10:12 PM
Hi!
You should probably use:
if (!EventSystem.current.IsPointerOverGameObject())
{
// Jump
}
But also use this for you be able to use "EventSystem":
using UnityEngine.EventSystems;
Your answer
Follow this Question
Related Questions
How is "Touch.tapCount" bounded? 1 Answer
detecting taps on 3d objects in iOS 6 Answers
Remove Y-axis clamp from Tap to Move, Drag to Look Script 0 Answers
Tapping doesn't work when played on iPhone 1 Answer
Ios Tap Function 1 Answer