Pause button triggers Player jump

Hi , I am creating a game where Player is running and jumping on mouse click. So I made Pause button for the game , but issue that I have now is that when Pause button is being pressed Player jumps and then game stops. How do I make my Pause button not interact with Player jumping ? I’m not that good with programming so this issue is hard to resolve by myself . Thanks for your time!

Player controls :

// Update is called once per frame
    void Update () {

        grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);

        if (transform.position.x > speedMilestoneCount)
        {
            speedMilestoneCount += speedIncreaseMilestone;

            speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;

            moveSpeed = moveSpeed * speedMultiplier;
        }




   
   

        myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y); //Controlls

        if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
            if (grounded) {
               
                myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
            }
        }

        if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton (0))
        {
            if (jumpTimeCounter > 0)
            {
                myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
                jumpTimeCounter -= Time.deltaTime;
            }
               
        }

        if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
            {
                jumpTimeCounter = 0;
            }


            if(grounded)
            {
                jumpTimeCounter = jumpTime;
            }

        myAnimator.SetFloat ("Speed", myRigidbody.velocity.x);
        myAnimator.SetBool ("Grounded", grounded);


    }

Pause script :

    public void PauseGame ()
    {
        Time.timeScale = 0f;
        pauseMenu.SetActive (true);
    }

You can try: if(!EventSystem.current.IsPointerOverGameObject()) in front of your code to allow jumping with a click.
That’s how I always prevented similar things :slight_smile:

2 Likes

Thank you , but how to make it work on touch screen ?

Hm, that’s a good question. My first instinct may be… delay the jump by 1 frame.
It doesn’t sound super ideal in my head… You’d have to set a variable when you click a button (to indicate a button was pressed), reset that at the appropriate place+time… and when you want to jump, you pass it to a coroutine that fires after 1 frame, if no button was pressed. lol
I do not know if that is the only way… :slight_smile:

So I found a way to fix it but now my animations all are broken , so maybe someone can help me out

// Update is called once per frame
    void Update () {
       

           

            grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);

            if (transform.position.x > speedMilestoneCount) {
                speedMilestoneCount += speedIncreaseMilestone;

                speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;

                moveSpeed = moveSpeed * speedMultiplier;
            }




   

            myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y); //Controlls


        if(!EventSystem.current.IsPointerOverGameObject (Input.GetTouch (0).fingerId)) {

            if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
                if (grounded) {
               
                    myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
                }
            }


            if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton (0)) {
                if (jumpTimeCounter > 0) {
                    myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
                    jumpTimeCounter -= Time.deltaTime;
                }
               
            }

            if (Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp (0)) {
                jumpTimeCounter = 0;
            }
           
            if (grounded) {
                jumpTimeCounter = jumpTime;
            }
       
       

            myAnimator.SetFloat ("Speed", myRigidbody.velocity.x);
            myAnimator.SetBool ("Grounded", grounded);

        }
    }

Okay, so the pointer over gameobject did in fact work ?
What is broken … was it better/working before?..
I think maybe you have to close the brace/bracket for the pointer overgameobject check just before your animator SetFloat and SetBool, also?

Plus, your jump logic is a little weird. It may be a different question, but what is the jumpTimeCounter for?
That code could probably be cleaned up a bit…