Userbitcoin4life1
Userbitcoin4life1

Reputation: 15

Game character won't double jump c#

I'm trying to create a 2D platform game in unity, when I try and make the character double jump, it won't work. I was wondering if i could get any help.

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float maxSpeed = 3;
    public float speed = 50f;
    public float jumpPower = 150f;

    public bool grounded;
    public bool canDoubleJump;

    private Rigidbody2D rb2d;
    private Animator anim;

    void Start ()
    {
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }


    void Update ()
    {

        anim.SetBool("Grounded", grounded);
        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));

        if(Input.GetAxis("Horizontal") < -0.1f)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if(Input.GetAxis("Horizontal") > 0.1f)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }

        if(Input.GetButton("Jump"))
        {
            if(grounded)
            {
                rb2d.AddForce(Vector2.up * jumpPower);
                canDoubleJump = true;
            }
            else
            {
                if (canDoubleJump)
                {
                    canDoubleJump = false;
                    rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
                    rb2d.AddForce(Vector2.up * jumpPower);

                }
            }
        }

    }

    void FixedUpdate()
    {
        Vector3 easeVelocity = rb2d.velocity;
        easeVelocity.y = rb2d.velocity.y;
        easeVelocity.z = 0.0f;
        easeVelocity.x *= 0.75f;

        float h = Input.GetAxis("Horizontal");

        //fake friction / easing x speed
        if(grounded)
        {
            rb2d.velocity = easeVelocity;
        }


        //moving player
        rb2d.AddForce((Vector2.right * speed) * h);

        //limiting speed
        if(rb2d.velocity.x > maxSpeed)
        {
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
        }

        if(rb2d.velocity.x < -maxSpeed)
        {
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
        }
    }
}

Upvotes: 0

Views: 177

Answers (1)

The problem is that you're checking if the Jump button is currently in the down state. Pressing and releasing the button often takes place over multiple frames (i.e. Update() is called many times in the duration of button press).

There are two ways you can fix this problem.

The easiest (and probably best) is to make this alteration:

if(Input.GetButtonDown("Jump"))

GetButtonDown only returns true for the frame in which the button was first pressed, and false until it is released and pressed again.

The other is to include a second variable that prevents the activation of the second block until after the button is released. This is less ideal, but shows what's going on behind the scenes of GetButtonDown.

var isButtonDown = false;

Update() {
        if(Input.GetButton("Jump"))
        {
            if(grounded)
            {
                rb2d.AddForce(Vector2.up * jumpPower);
                canDoubleJump = true;
                isButtonDown = true;
            }
            else if(!isButtonDown)
            {
                if (canDoubleJump)
                {
                    canDoubleJump = false;
                    rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
                    rb2d.AddForce(Vector2.up * jumpPower);

                }
            }
        }
        else {
            isButtonDown = false;
        }
    }

Note that this does not address the "fall off a platform and jump once" that the double-jump ability typically includes. I'll leave that as an exercise to the reader.

Upvotes: 1

Related Questions