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

Inconsistent Jump Height

Discussion in 'Scripting' started by Sushin, May 24, 2015.

  1. Sushin

    Sushin

    Joined:
    May 2, 2015
    Posts:
    25
    Hi guys.
    I'm having some trouble figuring out what's causing an inconsistent jump height issue with my controller. Sometimes if I tap space, what happens is a really tiny hop occurs instead of what the default minimum should be, JumpPowerMin, a value of 1 currently.

    As you can see below, if you hold space, the jump power charges, but I don't believe I'm having issues with that. The issues are with tapping the jump button quickly and having inconsistent, tiny jump heights when I'm specifically setting the minimum jump height. My suspicion is that it has something to do with the "is grounded" variable not being reliable, but if that's the case, what's the best way to correct for that?

    CC refers to Unity's Character Controller script.

    Code (csharp):
    1. void PlayerJump() {
    2.         if (CC.isGrounded) {
    3.             //stick to ground if grounded
    4.             MovDir.y = -CC.stepOffset / Time.deltaTime;
    5.  
    6.             //jump input
    7.             if (Input.GetButtonDown ("Jump")) {
    8.                 JumpCharge = true;
    9.             } else if (Input.GetButtonUp("Jump")){
    10.                 MovDir.y = (JumpChargePower * Time.deltaTime);
    11.                 //JumpChargePower = JumpPowerMin;
    12.                 JumpCharge = false;
    13.             }
    14.  
    15.             if(JumpCharge == true){
    16.                 if(JumpChargePower < JumpPowerMax){
    17.                     JumpChargePower += Time.deltaTime;
    18.                 } else if (JumpChargePower > JumpPowerMax){
    19.                     JumpChargePower = JumpPowerMax;
    20.                 } else if (JumpChargePower < JumpPowerMin){
    21.                     JumpChargePower = JumpPowerMin;
    22.                 }
    23.             } else {//if not charging a jump
    24.                 //JumpChargePower = JumpPowerMin;
    25.             }
    26.  
    27.         } else {//if player is not on ground
    28.             //apply gravity
    29.             MovDir.y += (-Gravity * Time.deltaTime);
    30.             JumpChargePower = JumpPowerMin;
    31.             JumpCharge = false;
    32.         }
    33.     }
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    If I had to guess, it's this line.

    Code (csharp):
    1. MovDir.y=(JumpChargePower *Time.deltaTime);
    Time.deltaTime is something you multiply into a variable to normalize it versus the framerate. Without having the ability to normalize things against the framerate, you'd run into issues in making sure your game plays the same at 20 FPS as at 60 FPS. For example, if you say that your player moves 1 meter every frame, he moves three times as fast at 60 FPS than 20 FPS. To solve this, anything occurring over a series of frames gets multiplied by Time.deltaTime, which is "The time in seconds it took to complete the last frame."

    Anyways, when the jump button is released you set the MovDir.y in just one frame, so there's no need to normalize it.
     
    Sushin likes this.
  3. Sushin

    Sushin

    Joined:
    May 2, 2015
    Posts:
    25
    That seemed to work. I guess I wasn't really aware of where I should be using that. Thanks a lot.