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. Dismiss Notice

Question Unity 2D Fluid Jumping

Discussion in '2D' started by rizenmusic, Feb 9, 2021.

  1. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    So I'm building a platformer and right now I'm experimenting with jumping and movement. Everything looks good except one thing: sometimes when I hit the ground and press "jump" my character isn't jumping. I've been looking into Hollow Knight jumping system and found out that in their game, if I hit the jump button even when I'm not on the ground, the player would still jump right after collision with it.

    Somehow they managed to do that the player isn't able to jump repeatedly while holding the jump button, but will jump on collision with ground even if you press "jump" buttor right before collision happens which creates smooth platforming experience. How to achieve the same result?

    My jumping code:

    Code (CSharp):
    1.    void playerJump()
    2.     {
    3.         //Checking conditions for double jumping if player is in falling condition
    4.                if (isFalling && doubleJumpReady)
    5.         {
    6.             if (Input.GetButtonDown("Jump") && (currentJumpsValue > 0))
    7.             {
    8.                 isJumping = true;
    9.                 jumpTimeCounter = jumpTime;
    10.                 doubleJumpReady = false;
    11.                
    12.             }
    13.             if (Input.GetButton("Jump") && jumpTimeCounter > 0)
    14.             {
    15.                 rdBody2D.velocity = new Vector2(rdBody2D.velocity.x, jumpForce);
    16.                 jumpTimeCounter -= Time.deltaTime;
    17.                
    18.             }
    19.             if (Input.GetButtonUp("Jump"))
    20.             {
    21.                 isJumping = false;
    22.                 jumpTimeCounter = 0;
    23.                 currentJumpsValue--;
    24.             }
    25.         }
    26.         //Conditions for the first jump.
    27.         if (Input.GetButtonDown("Jump") && (currentJumpsValue > 0) && !isFalling)
    28.         {
    29.             isJumping = true;
    30.             jumpTimeCounter = jumpTime;
    31.             Debug.Log("Jumped!");
    32.         }
    33.         if (Input.GetButton("Jump") && jumpTimeCounter > 0)
    34.         {
    35.             rdBody2D.velocity = new Vector2(rdBody2D.velocity.x, jumpForce);
    36.             jumpTimeCounter -= Time.deltaTime;
    37.         }
    38.         if (Input.GetButtonUp("Jump"))
    39.         {
    40.             isJumping = false;
    41.             jumpTimeCounter = 0;
    42.             currentJumpsValue--;
    43.         }
    44.     }
     
  2. Kalliber95

    Kalliber95

    Joined:
    Mar 27, 2015
    Posts:
    19
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Start at 8:03 in this video, it explains it well. But Kalliber is right as well.

     
  4. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    That's not very convenient because jump buffering will be different based on falling speed instead of time.

    Found a tutorial that uses this method:

    Code (CSharp):
    1.         if (Input.GetButtonDown("Jump"))
    2.         {
    3.             jumpTimer = Time.time + jumpDelay;
    4.         }
    5.  
    6.         if (jumpTimer > Time.time)
    7.         {
    8.             PlayerJump();
    9.         }
    It works BUT I can't implement a double jump function into this. If I do something like this (don't mind the spelling):

    Code (CSharp):
    1. PlayerJump()
    2. if (onGround)
    3. then jump
    4. if else (!onGround && isAbleToDoubleJump)
    5. them jump while in the air or falling state
    6.  
    The code just doesn't work. I'm trying to figure out my jump functions with double jump for days now and it's still not working.
    What I need exactly:
    Different height with long and short button tap: done with changing gravity while in the air
    Coyote time and input buffering: done with timers
    Double jump: can't get it to work with input buffering.

    Help please :)
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Here is a video for jumping higher depending on how long button is pressed.


    As for you not liking the raycast method above, what does it matter about the falling speed? A raycast would actually be much better than a timer because when it touches the ground, it means you can press the jump input again, regardless of how fast or slow they are falling, and that is what you want. You want it to be smooth so if they press the jump button a tad too early, it will still register. Another thing, if you did a timer and mixed it with varied jump heights, that timer would have to be dynamic and change values depending on how high the character jumped. The raycast just makes way more sense than a timer.

    I have no idea what Coyote time is.