Is it possible to not jump when I click a button? (698392)

I’m currently trying to replicate flappy bird so that my bird jumps when I click on the screen.

if (Input.GetMouseButtonDown(0))
{

rb2d.velocity = Vector2.zero;
rb2d.AddForce(new Vector2(0, upforce));

}

this is the script attached to my bird but the problem is, I also have a pause button on the top right of my screen and whenever I click the pause button, the bird jumps too… is there a way of tagging the button or some function I could use so that this doesnt happen?

A pseudo code i’m after looks something like

if(input.getmousebuttondown(0) && isNotaButton) then jump.

Any help is really appreciated thanks!!

You can try something like this to see if the mouse pointer is NOT over a UI object.

if (!EventSystem.current.IsPointerOverGameObject())
{
//do something
}

The way I always solve this is:

  1. Put a giant invisible Image on Canvas that covers the whole screen (but behind other UI elements).
  2. Add an EventTrigger to that thing, that intercepts the events you care about (PointerDown in this case), and calls some event on your game controller (like Flap() in your bird controller).

Because the image is behind everything else, it (and so your flappy bird) will only get the event when some higher UI element (e.g. the Pause button) doesn’t handle it first.

For touch controls, you may have to use the Touch.fingerID as a parameter to IsPointerOverGameObject for it to work.