Unity 5 - how to add a double jump to the default third person controller script ?

Hey, I’ve been trying to add a double jump to Unity’s default third person controller script for a couple of days now with no success. I’ve tried to create a boolean to state that double jump is true after performing a normal jump and then false after the double jump but It ends up allowing me to jump and an infinite amount of times, I’ve also tried the counter method which ended with the same result. I’m going to assume I’m just missing something pretty obvious. but I was just wondering if anyone had a snippet that I could use if they have managed to achieve this I would be very grateful. Or even a bit of advice on something I may be missing.
Thanks for your time.

Ok. Here’s a little walkthrough of how to add a simple double jump to the default third person character.

OK. So let’s start disecting the third person character!

Open up ThirdPersonCharacter.cs and ThirdPersonUserControl.cs.

Firstly, you’ll need to scan through the code and look for where the jump code starts, if you’re lucky, there’ll be a Jump() method, which makes things very easy. Otherwise, try hitting CTRL + F and searching for “grounded” or “ground”. There should be a boolean that states whether the character is touching the ground or not. We can use this to narrow down where the jump code starts.

At line 46 I found the Move() method, which takes in a vector from the user and then moves them in that direction.

Inside the method is this code, which points to another method:

  if (m_IsGrounded)
      HandleGroundedMovement(crouch, jump);

And inside the HandleGroundedMovement() method, there’s also this code:

// check whether conditions are right to allow a jump:
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded")) {
    // jump!
    m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower,  m_Rigidbody.velocity.z);
    m_IsGrounded = false;
    m_Animator.applyRootMotion = false;
    m_GroundCheckDistance = 0.1f;
}

But finally, there’s one more thing that should be modfied to achieve the double jump. In the ThirdPersonUserControl.cs script, you’ll see that in a simple Update() loop there’s an IF statement that detects jump input:

 private void Update() {
     if (!m_Jump)
         m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
 }

In a nutshell, this script detects user input and then feeds it into the ThirdPersonCharacter.cs script.

So at the top of this code, underneath m_Jump, add a public integer called “jumpState”. This will count the amount of jumps we have done in midair. Since it’s public, we can also see it in the inspector. Useful for debugging.

public int jumpState;

What we will do is we will have the jumpState integer set to how many jumps you have done in midair. When jumpState goes over a limit, you can no longer jump in the air.

Ok so let’s get this code to work. Modify the Update() loop to look like this:

private void Update() {
    if (!m_Jump) {
        m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
        if (m_Jump)
            jumpState++;
    }
    if (m_Character.m_IsGrounded)
        jumpState = 0;
}

Now open up the ThirdPersonCharacter.cs code and add this argument to the Move() method:

int jumpState = 0

So now the Move() method should look like this:

public void Move(Vector3 move, bool crouch, bool jump, int jumpState = 0) {

Then go back to the ThirdPersonUserContro.csl script, and in the FixedUpdate() loop, add this argument to the line that calls the move function. So now that line should look like this:

m_Character.Move(m_Move, crouch, m_Jump, jumpState);

Finally, go back into ThirdPersonCharacter and make the m_IsGrounded variable public. Like this:

public bool m_IsGrounded;

Now if you test the scene, you should notice that the value of jumpAmount sits at 0 when the character is touching the ground, and when you jump in mid air, that value increases.

Now to make the character jump in mid air!

So in the ThirdPersonCharacter.cs script, go down to the move function and add this line just before the if (m_IsGrounded) statement:

if (jump == true && jumpState < 2) {
    HandleGroundedMovement(crouch, jump);
}

Lastly, go down to the HandleGroundedMovement() method, and remove the animation check there, so now it will look like this:

void HandleGroundedMovement(bool crouch, bool jump) {
    // check whether conditions are right to allow a jump:
    if (jump && !crouch) {
        // jump!
        m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
        m_IsGrounded = false;
        m_Animator.applyRootMotion = false;
        m_GroundCheckDistance = 0.1f;
   }
}

If done correctly, you will be able to jump a second time in mid air! To increase the number of jumps the character can do in mid air, just increase the number ‘2’ here:

if (jump == true && jumpState < 2) {
    HandleGroundedMovement(crouch, jump);
}

If you want to, you can link this number to a variable and then have an upgrade system, or an on screen counter for how many jumps you have left.

Hope this helped. Let me know if the code works for you!

If not, just comment, and I’ll help you sort it out!

@SimplyMelee Hi, sorry to bother you but I downloaded the test scene you (the one with the capsule + cowboy hat) and am wondering how can I modify it so that when I turn from side to side, the character rotates as well? Like how when we walk and turn we actually turn to face the we’re walking.

You have been a great help though thank you so much!

Since Unity deprecates code every update, this double jump code @SimplyMelee does not work anymore.

Is there a newer way how to make Ethan double jump in 2019?