How to add double jump into mobile game?

Hello! For several days I tried to implement double jumping in my mobile game. I created a UI image, that acts like a button for jumping and it has this code inside:

public class JoyButtonScript : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
    public void OnPointerDown(PointerEventData eventData){
        jumpPressed = true;
    }
    public void OnPointerUp(PointerEventData eventData){
        jumpPressed = false;
    }
}

I.e when I the press button, jumpPressed becomes true and my player jumps, thanks to the script PlayerMovement attached to player:

public class PlayerMovement : MonoBehaviour
{
   private int extraJumps;
   public int extraJumpsNumber;

void Start()
    {
        extraJumps = extraJumpsNumber;
    }
void Update()
    {
       if(Input.GetKeyDown(KeyCode.Space) || jumpPressed){
           Jump();
       }else if((Input.GetKeyDown(KeyCode.Space) || jumpPressed) && extraJumps > 0 ){
           Jump();
           extraJumps--;
    }
       }
void Jump(){
        rb.velocity = Vector2.up * jumpSpeed;
   }

My problem is that when I press the button, second jump is called at the same time, as first jump, so theres an illusion of player jumping once, but actually player jumped twice. But if instead of the button I press Space, the player jumps once and if I press Space again, player jumps second time, just like it should work. Through some research I finally understood, that when I call Input.GetKeyDown it becomes True for a single frame and then immediately becomes False again, while my jumpPressed is True, until I release it, and thats the exact reason why code of (else if) statement is called instantly after the main (if) statement.
I tried to recreate code of Input.GetKeyDown, so my button would become True right when I press it, not hold down. I tried various methods to make jumpPressed bool to become True right during pressing, but none of them worked. Hope, you can help me and thanks!

Generally don’t write code this way:

       if(Input.GetKeyDown(KeyCode.Space) || jumpPressed){
           Jump();
       }else if((Input.GetKeyDown(KeyCode.Space) || jumpPressed) && extraJumps > 0 ){
           Jump();
           extraJumps--;

That’s completely impossible to reason about. I can’t tell where one thing begins and another ends, it’s just one big long hairy statement.

Instead, break the data processing into steps:

  • gather the user input intentions to local variables
    ----------> gather from keyboard when keyboard present
    ----------> gather from screen touches when on mobile

  • process the user input based on the above flags

Once you have broken that apart, now you can actually start putting in Debug.Log() calls to find out where things are going wrong. On mobile you can also see the console (google for how) at runtime, and figure out where things are failing.

Sorry, mate. I forgot I even created this thread.I did as you said and used something like:

if(Input.GetKeyDown(KeyCode.Space){
Debug.Log("1");
}

if(jumpPressed){
Debug.Log("2");
}

I noticed, that when I press Space, debug.log only works once & if I press jump button(i.e jumpPressed becomes true), debug.log works several times, like 5 or 6 is a single frame. I guess the problem is inside of OnPointerDown of JoyButton script. It works the way, that while button is pressed, it keeps “jumpPressed” bool as True.
So that’s the exact reason, why (else if) statement is called immidiately after the main (if) statement. I need to create code similar to how Input.GetKeyDown works and attach it to my JoyButtonScript, i.e jumpPressed must become True at the same frame I press the jump button and then immidiately must become False again. The only similar thing I found is OnPointerClick, but it works twice: when pressed and when released, which causes my Player to jump several times, instead of single jump only, so I’m still struggling to solve this problem