3
\$\begingroup\$

I use Unity 5 and I have an issue with my game, when I click on the Pause button my character jumps before the game is paused. My script detects the click for jump before the pause button so for that I add gameObject.tag but doesn'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:

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

Look 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\$
  • \$\begingroup\$ Well I deleted my answer because I didn't understand the problem. Hopefully someone else comes along with an answer. \$\endgroup\$ – Daniel Sep 28 '15 at 1:31
  • \$\begingroup\$ why the vote down? \$\endgroup\$ – Markop Sep 28 '15 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 Sep 28 '15 at 4:21
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\$
  • \$\begingroup\$ It still doesn't work \$\endgroup\$ – Markop Sep 28 '15 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 Sep 28 '15 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 Sep 29 '15 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 Sep 29 '15 at 1:16
1
\$\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\$
  • \$\begingroup\$ It doesn't work \$\endgroup\$ – Markop Sep 28 '15 at 13:52
  • \$\begingroup\$ Can you clarify what's happening on the screen right now? \$\endgroup\$ – Elmar Talibzade Sep 29 '15 at 10:34
  • \$\begingroup\$ When I write your script, nothing change just like I didn't do anything \$\endgroup\$ – Markop Sep 29 '15 at 14:33
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.