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

Double jump in a 2D game

Discussion in 'Scripting' started by wiredbyalex, Jun 13, 2015.

  1. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    I am attempting to give my player a double jump in my 2D game. All the stuff that is in comment form in the script is what I attempted to use to perform my double jump.

    Code (CSharp):
    1. void StateInAir()
    2.     {
    3.         HandleHorizontalInput ();
    4.  
    5.         if (canJump) {
    6.             if (timeHoldingInput < maxJumpBtnHoldTime && Input.GetKey(KeyCode.Space))
    7.             {
    8.                 timeHoldingInput += Time.deltaTime;
    9.                 GetComponent<Rigidbody2D> ().velocity += new Vector2 (0, thePlayer.JumpAccel ());
    10. //                if(Input.GetKeyUp(KeyCode.Space) && doublejump == true)
    11. //                {
    12. //                    SetState(PlayerStates.ENTER_JUMP);
    13. //                    if (timeHoldingInput < maxJumpBtnHoldTime && Input.GetKey (KeyCode.Space))
    14. //                    {
    15. //                        timeHoldingInput += Time.deltaTime;
    16. //                        GetComponent<Rigidbody2D> ().velocity += new Vector2 (0, thePlayer.JumpAccel ());
    17. //                        doublejump = false;
    18. //                    }
    19. //                }
    20.             }
    21. //            else if( doublejump == true)
    22. //            {
    23. //                timeHoldingInput = 0;
    24. //                doublejump = false;
    25. //            }
    26.             else
    27.             {
    28.                 canJump = false;
    29.                 //SetState(PlayerStates.IDLE);
    30.             }
    31.         }
    32.         else
    33.         {
    34.             CheckForGround();
    35.             if( onGround)
    36.             {
    37.                 HandleLandOnGround();
    38.                 doublejump = true;
    39.             }
    40.         }
    41.     }
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Well, hard to say what's wrong with it having all those variables with unknown source/values. :)
    canJump
    maxJumpBtnHoldTime
    doublejump
    A good start would be to add some Debug.Log statements to see what are the actual values of those variables and if your double jump code does trigger.
     
  3. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    Oh sorry. I didn't add the entire code thinking it would be easier for people to read what I am talking about, I guess not. :) Here are my variables. I will try the debug and see what happens.
    Code (CSharp):
    1. private enum PlayerStates
    2.     {
    3.         IDLE,
    4.         RUN,
    5.         ENTER_JUMP,
    6.         IN_AIR,
    7.  
    8.         NUM_STATES
    9.     }
    10.    
    11.     //Exposed Variables
    12.     [SerializeField]
    13.     private float maxJumpBtnHoldTime = 0.28f;
    14.     [SerializeField]
    15.     private float timeHoldingInput = 0.0f;
    16.     [SerializeField]
    17.     private Transform groundCheck;
    18.     [SerializeField]
    19.     private LayerMask walkableLayer;
    20.     [SerializeField]
    21.     private float groundCheckRadius = 0.1f;
    22.     [SerializeField]
    23.     private GameObject spawnPoint;
    24.     //Private Variables
    25.     Player thePlayer;
    26.     PlayerStates curState;
    27.     Dictionary<PlayerStates, Action> fsm = new Dictionary<PlayerStates, Action>();
    28.  
    29.     private bool canJump = true;
    30.     private bool onGround = false;
    31.     private int healthAmount = 100;
    32.     private int damageAmount= 0;
    33.     private bool doublejump = true;