Question by
McGame · Jul 20, 2016 at 04:56 PM ·
c#jumping object
Preventing munliple jumps
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
Comment
Answer by Rempork · Jul 20, 2016 at 06:02 PM
Hi,
you should use this instead
void OnCollisionEnter2D(Collision2D coll) {
canjump = true;
}
but have a look at the PlatformController2d in the Standard Assets
(a problem i see with your version is that each collision will count so if you jump at a wall you can jump again because that is alsow a collision)
Your answer
Follow this Question
Related Questions
Jump OnCollision() Script not working 0 Answers
C#: How do you make a 2D game object jump? 1 Answer
stick to roof of object when jumping 0 Answers
My simple jump script is only letting me jump once? 1 Answer
Player does not jump? 0 Answers