0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour {

public float jumpSpeed;

private Rigidbody2D _rigidbody;

private bool _isGrounded = false;

// Use this for initialization
void Start () {
    _rigidbody = GetComponent<Rigidbody2D> ();
}

// Update is called once per frame
void Update () {
    //if the jump button was pressed
    if (Input.GetButtonDown ("Jump") && _isGrounded) {
        _rigidbody.velocity = new Vector2 (
            _rigidbody.velocity.x,
            jumpSpeed
        );
    }
}

void OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.tag == "Ground") {
        _isGrounded = true;
    }
}

void OnCollisionLeave2D(Collision2D collision) {
    if (collision.gameObject.tag == "Ground") {
        _isGrounded = false;
    }
}

}

Its supposed to check if the player is on the ground, but it still lets my character jump unlimited times in the game.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Isn't the method "OnCollisionExit2D" not "OnCollisionLeave2D"? \$\endgroup\$
    – DMGregory
    Commented Mar 4, 2017 at 5:44

1 Answer 1

0
\$\begingroup\$

Because you're not setting your bool _isGrounded to false anywhere. As @DMGregory said, there is no such method as OnCollisionLeave2D defined in Unity API, thus it won't be executed unless called explicitly, which means, the bool _isGrounded will remain true and your player will continue to recieve jump input. You need to change it to OnCollisionExit2D.

\$\endgroup\$
0

You must log in to answer this question.

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