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\$
4
  • \$\begingroup\$ totalJumps can be ignored, forgot to take that out \$\endgroup\$ Commented Jan 18, 2017 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
    Commented Jan 18, 2017 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
    Commented Jan 18, 2017 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
    Commented Jan 19, 2017 at 0:10

1 Answer 1

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\$
1
  • \$\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\$ Commented Jan 20, 2017 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .