0

I have a mobile game where the only thing you have to do is to tap to jump,now i added a pause menu and when i click it the player still jumps,after i click on resume button the player also jumps and makes the game annoying

How to fix?

Code:

 public void Pause()
{    
  Time.timeScale = 0;
  panel.SetActive(true);
         
    
}
public void Resume()
{
    Time.timeScale = 1;
    panel.SetActive(false);
    
}

And for jump if u ask(it's called in void update):

 void Jump()
{
    if (Input.GetMouseButtonDown(0)) 
    {
         rb.AddForce(Arrow.transform.right * -ImpulseForce * Time.fixedDeltaTime, ForceMode2D.Impulse);
    }
}
Nelu Belei
  • 37
  • 1
  • 6

1 Answers1

1

Depending on your setup, one easy way is to check if the current event is over an UI GameObject before adding the force to your rigidbody, using EventSystem.current.IsPointerOverGameObject():

void Jump()
{
    if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
    {
         rb.AddForce(Arrow.transform.right * -ImpulseForce * Time.fixedDeltaTime, ForceMode2D.Impulse);
    }
}

Alternatives would be to use raycasts to check where a click went (UI or not) or implement one of the IPointerClickHandler / IPointerDownHandler interfaces.

schneebuzz
  • 348
  • 1
  • 2
  • 10
  • thanks a lot it worked, i was trying to find a same ideea to check if the button is clicked but i didn't know of this, ty! – Nelu Belei Jul 30 '20 at 11:54
  • Found this one after a quick search. Let us know if it helps: https://answers.unity.com/questions/1115464/ispointerovergameobject-not-working-with-touch-inp.html – schneebuzz Jul 30 '20 at 15:29