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

2d Platformer Jumping Issue plz help.

Discussion in 'Scripting' started by soczekyt, Mar 10, 2020.

  1. soczekyt

    soczekyt

    Joined:
    Mar 10, 2020
    Posts:
    3
    hi guys I have problem with my jumping code for 2d platformer. it have multipe jumps feature but when i start jumping from fall i have 1 less jump. I think solution can be when jumping from ground -1 jump or when from fall +1 jump but i dont know how to do that. Can someone help me figure it out? Thanks.<3
    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);
    4.         if(isGrounded == true)
    5.         {
    6.             extraJumps = 2;
    7.         }
    8.         if ((Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0) || (Input.GetKeyDown(KeyCode.W) && extraJumps > 0))
    9.         {
    10.             rb.velocity = Vector2.up * jumpForce;
    11.             extraJumps--;
    12.         }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You could add an else if after the grounded check:
    Code (csharp):
    1.  
    2.  private void Update()
    3.     {
    4.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);
    5.         if(isGrounded == true)
    6.         {
    7.             extraJumps = 2;
    8.         } else if(extraJumps > 1) // we know we aren't grounded
    9.         {
    10.             extraJumps = 1;
    11.         }
    12.         if ((Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0) || (Input.GetKeyDown(KeyCode.W) && extraJumps > 0))
    13.         {
    14.             rb.velocity = Vector2.up * jumpForce;
    15.             extraJumps--;
    16.         }
     
  3. soczekyt

    soczekyt

    Joined:
    Mar 10, 2020
    Posts:
    3
    That only -1 the jump count and ive figured it out myself i needed to - counter when the button is released.
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         if(groundCheck.GetComponent<GroundCheck>().GHit)
    4.         {
    5.             extraJumps = 2;
    6.         }
    7.  
    8.         if((Input.GetKeyDown(KeyCode.W) && extraJumps > 0) || (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0))
    9.         {
    10.             rb.velocity = Vector2.up * jumpForce;
    11.         }
    12.  
    13.         if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
    14.         {
    15.             extraJumps--;
    16.         }
    17.     }