0
\$\begingroup\$

I have been making a 2D game in Unity, with a group of mine. We have made a character and are able to make him run and jump. Currently, I have been trying to make him double jump so someone can't just spam the jump button and go flying, but nothing has been working.

I've been through about at least 10 different tutorials on it, but nothing has worked. How do I make my character double jump?

public float speed;
public float jump;

float moveVelocity;
private Rigidbody2D myRigidBody;
private bool grounded = true;

void Start () 
{
    myRigidBody = GetComponent<Rigidbody2D> ();
}

void Update () 
{
    //constant run
    myRigidBody.velocity = new Vector2(speed, myRigidBody.velocity.y);

    if (Input.GetKeyDown (KeyCode.Space)) 
    {
        if (grounded) 
        {
            myRigidBody.velocity 
                = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
        } 
    }
}
\$\endgroup\$
  • \$\begingroup\$ totalJumps can be ignored, forgot to take that out \$\endgroup\$ – Matthew miner Jan 18 '17 at 20:53
  • \$\begingroup\$ Grounded never changes? All you need to do is keep a counter for the number of jumps. Don't let it jump if it's over the allowed number of times and reset the counter when the character is back on it's feet. \$\endgroup\$ – Madmenyo Jan 18 '17 at 21:01
  • \$\begingroup\$ hm Madmenyo's method might be better than mine, since you can change it from a double jump to a single jump during testing just by changing the max jumps value \$\endgroup\$ – jhocking Jan 18 '17 at 21:07
  • \$\begingroup\$ G'day :) welcome to game dev! Please note the tag changes I have made. You should always use the unity tag, if your asking about Unity, as it increases your chances of the right people seeing it. I interpret your script as C# (apologies if I am incorrect). Note that UnityScript applies to the JavaScript variant; you should not use it for c# code. If I took out the wrong tag, you can hit edit and correct my mistake. \$\endgroup\$ – Gnemlock Jan 19 '17 at 0:10
1
\$\begingroup\$

Change 'totalJumps' to 'doubleJumped' and replace that condition with:

if (Input.GetKeyDown (KeyCode.Space)) 
{
    if (grounded || !doubleJumped) {
        myRigidBody.velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
        doubleJumped = !grounded;
    }
} else if (grounded) {
    doubleJumped = false;
}

That's saying to jump either when grounded or you haven't double jumped yet, and to reset the double jump variable when on the ground.


incidentally, I assumed 'grounded' is being detected, although that's not in your code sample. If needed, do a raycast down to detect when standing on the ground, explained more here: http://answers.unity3d.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html

\$\endgroup\$
  • \$\begingroup\$ Thanks for the info, I was working on implementing a ground detection and now it all works how we wanted it to, and again, thank you very much! \$\endgroup\$ – Matthew miner Jan 20 '17 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.