4
\$\begingroup\$

I have an issue with my Unity 5 game. When I click on the Pause button, my character jumps before the game is paused.

My script detects the click to trigger the jump jump before the pause button. I tried adding a tag check but that didn't work:

void Update () {
    if (Input.GetKeyDown (KeyCode.Space) && Time.timeScale == 1 || Input.GetMouseButtonDown (0) && Time.timeScale == 1 && gameObject.tag != "Pause") {
        GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
    }

http://makeagif.com/i/bmCKQ3:

Flappy bird animation

You can see in this gif, when I click on the pause button it jumps and pauses directly.

How can I click on the Pause button without making my player jump?

\$\endgroup\$
3
  • \$\begingroup\$ Well I deleted my answer because I didn't understand the problem. Hopefully someone else comes along with an answer. \$\endgroup\$
    – Daniel
    Commented Sep 28, 2015 at 1:31
  • \$\begingroup\$ why the vote down? \$\endgroup\$
    – Markop
    Commented Sep 28, 2015 at 2:45
  • 1
    \$\begingroup\$ gameObject.tag refers to the tags on this.gameObject, ie. your bird. It does not refer to the tags on the object that has been clicked. Also, watch out for doing GetComponent<> every frame. It's usually better to do that once in Start, and then cache the reference in a member variable to use later. \$\endgroup\$
    – DMGregory
    Commented Sep 28, 2015 at 4:21

5 Answers 5

2
\$\begingroup\$

I had the same issue when I was doing a game. What basically happens is that your script detects if there is a mouse click, including when you click on UI components.

There is a solution to that fortunately:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class PlayerControl : MonoBehaviour {

    void Update () {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject(0))
        {
        MovePlayer();
        }
    }
}

So now every-time you tap on a screen unity checks if there is no UI element (buttons, panels etc.) where you tapped before moving your player.

Hope this helps! :)

\$\endgroup\$
3
  • \$\begingroup\$ It doesn't work \$\endgroup\$
    – Markop
    Commented Sep 28, 2015 at 13:52
  • \$\begingroup\$ Can you clarify what's happening on the screen right now? \$\endgroup\$ Commented Sep 29, 2015 at 10:34
  • \$\begingroup\$ When I write your script, nothing change just like I didn't do anything \$\endgroup\$
    – Markop
    Commented Sep 29, 2015 at 14:33
0
\$\begingroup\$

Ok, the .gif makes it make more sense. I think this is what you're looking for:

void Update () {
    Vector2 buttonDimensions = new Vector2(50, 50);
    Rect pauseButtonPosition = new Rect(Screen.width - buttonDimensions.x, 0, buttonDimensions.x, buttonDimensions.y);

    bool mouseOnPauseButton = pauseButtonPosition.Contains(Input.mousePosition);

    if (!mouseOnPauseButton && Input.GetKeyDown(KeyCode.Space) && Time.timeScale == 1 || Input.GetMouseButtonDown(0) && Time.timeScale == 1 && gameObject.tag != "Pause") {
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
    }
}

So basically if the mouse is on the pause button when you try to jump, it wont.

\$\endgroup\$
4
  • \$\begingroup\$ It still doesn't work \$\endgroup\$
    – Markop
    Commented Sep 28, 2015 at 13:52
  • \$\begingroup\$ You'll have to adjust the pause button position manually. If this doesn't work, I have to say that you're issue is somewhere else. \$\endgroup\$
    – Daniel
    Commented Sep 28, 2015 at 23:45
  • \$\begingroup\$ Thank you I change it manually and it works, and also is it good for performance to remove Vector2 buttonDimensions = new Vector2(50, 50); and just write the position and dimensions in Rect \$\endgroup\$
    – Markop
    Commented Sep 29, 2015 at 0:48
  • \$\begingroup\$ Technically yes, but the difference would be so minuscule that it wouldn't matter. Generally it's better to have more readable code than slightly faster code. \$\endgroup\$
    – Daniel
    Commented Sep 29, 2015 at 1:16
0
\$\begingroup\$

I had trouble with Daniel's answer in my Phone, but I have found the answer for the problem it

if (Input.GetMouseButtonDown (0)) {    
if (UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject) return;
    GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
}

Thanks to Georgedarocha, source: http://forum.unity3d.com/threads/ispointerovergameobject-ignoring-some-objects.277825/

\$\endgroup\$
0
\$\begingroup\$

For me at least, the below solution worked just fine.. Elmar had it almost right. Hope it helps the next person to find this!

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class PlayerControl : MonoBehaviour {

    void Update () {
        if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false)
        {
        MovePlayer();
        }
    }
}

Slight difference for smartphone games: https://docs.unity3d.com/2018.2/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

\$\endgroup\$
0
\$\begingroup\$

While the other answers “Hack” a solution for that individual button, it doesn’t fix the overall problem of a click propagating after it’s been detected by a script that uses it.

The input system can “consume” events. So that they are used up when required.and don’t get passed on to other objects.

For example Event.Use method.

There is a good discussion of this on the following thread:

https://forum.unity.com/threads/consuming-inputs-have-a-few-game-objects-handle-inputs-and-make-others-ignore-inputs-during-1-frame.348189/

You could also roll your own system where you can prioritize clicks to certain layers/ objects and if it gets “used” then it doesn’t get passed on to the next level of priority.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .