I have a problem with my jumping code. It jumps once then doesn’t jump again. Any assistance?
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public int speed;
private Rigidbody2D rb2d;
public int JumpSpeed;
private int jcnt;
private bool canjump = true;
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
void Jump () {
rb2d.AddForce(Vector2.up * JumpSpeed);
canjump = false;
}
void OnCollisionEnter () {
canjump = true;
}
void Update () {
float mh = Input.GetAxis ( "Horizontal" );
Vector2 movement = new Vector2 (mh, 0.0f);
rb2d.AddForce (movement * speed);
if (Input.GetButtonDown ("Jump") && canjump == true ) {
Jump ();
}
}
}
i could give you any other useful information if you like