Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Double Tap / Double Jump not working properly?

Discussion in 'Scripting' started by CatDoge, Nov 25, 2019.

  1. CatDoge

    CatDoge

    Joined:
    Aug 8, 2014
    Posts:
    13
    So this is my code for handling jumps and doublejumps but somehow it is not working how it should :/

    Sadly Unity Remote doesnt work for me so I cant debug my vars.

    It is actually an easy problem, but I can´t solve it :/

    Code (CSharp):
    1.      
    2.  
    3.         if (Input.touchCount > 0)
    4.         {
    5.             // get the first one
    6.             Touch firstTouch = Input.GetTouch(0);
    7.             TapCount += 1;
    8.  
    9.  
    10.             if (firstTouch.position.x > screenCenterX && firstTouch.position.x < Screen.width - screenCenterX)
    11.             {
    12.                 if (isG())
    13.                 {
    14.                     rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    15.                     doublejump = true;
    16.                 }
    17.                 if (isG() == false && doublejump == true)
    18.                 {
    19.                     rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    20.                  
    21.                     doublejump = false;
    22.                 }
    23.  
    24.             }
    25.  
    26.  
    27.         }
    28.  
    29.  
     
  2. ZaffreSheep

    ZaffreSheep

    Joined:
    Aug 26, 2018
    Posts:
    32
    From a quick look, I'm thinking whats happening is that the double jump is happening right after, because you aren't checking the state of the Touch(ie. whether it just touched the screen, it just let go, moving on the screen, etc etc).

    Take a look at this, it might prove useful. You'll be able to check the phase of the touch, so you can wrap the jump code within an if statement that checks if the touch has begun.
    https://docs.unity3d.com/ScriptReference/TouchPhase.Began.html
     
  3. CatDoge

    CatDoge

    Joined:
    Aug 8, 2014
    Posts:
    13

    Thank you for the reply!

    This is my final solution but sometimes it doesnt recognize the 2nd tap somehow. :/

    Code (CSharp):
    1.  
    2.             if (firstTouch.phase == TouchPhase.Began)
    3.             {
    4.                 if ((firstTouch.position.x > screenCenterX && firstTouch.position.x < Screen.width - screenCenterX) && IsGrounded() && doublejump == false)
    5.                 {
    6.                     //First Jump
    7.                     rb.velocity = (jump * jumpForce);
    8.                    
    9.                     doublejump = true;
    10.                 }
    11.                 else if ((firstTouch.position.x > screenCenterX && firstTouch.position.x < Screen.width - screenCenterX) && doublejump == true)
    12.                 {
    13.                     //Double Jump
    14.                     rb.velocity = (jump * jumpForce);
    15.                    
    16.                     doublejump = false;
    17.                 }
    18.             }
    19. }
     
  4. ZaffreSheep

    ZaffreSheep

    Joined:
    Aug 26, 2018
    Posts:
    32
    Hmm. I can't see anything that looks out of place, that's a little odd, so I'll just list a couple of notes:
    • Might get better results if you set double jump to true on TouchPhase.Ended, but I'm not sure if that's really necessary.
    • Make sure to reset the double jump variable when you land back on the ground, that would mess up your doublejump consistency.
    • Due to Unity Remote not working for you, you could always try to code some makeshift debug visual, like changing the players material color, or displaying some visual text UI in the game screen to show what doublejump is set to.
    Wish I could offer more clear help, but I haven't messed with TouchPhases too much. I hope ya figure it out, man.
     
    CatDoge likes this.