- Home /
2D platform, jump problem
Hi there, I'm using a rigidbody as player and I'm having problems with the jump function... Sometimes when I press the jump button it simply doesn't work, I believe it have something to do with the jumpEnabled variable, any help is very welcome!
using UnityEngine; using System.Collections;
public class playerMovement : MonoBehaviour {
public float maxVelocity;
public float jumpStrenght;
private Vector3 moveVector;
private bool jumpEnabled;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if(Input.GetAxis("Horizontal") != 0){
moveVector = new Vector3(Input.GetAxis("Horizontal")*Time.deltaTime*maxVelocity,rigidbody.velocity.y,rigidbody.velocity.z);
}else{
moveVector = new Vector3(0,rigidbody.velocity.y,rigidbody.velocity.z);
}
rigidbody.velocity = moveVector;
if(Input.GetButtonDown("Jump") && jumpEnabled){
jump (1);
}
}
void OnCollisionEnter(Collision collision){
if(collision.collider.CompareTag("terrain")){
jumpEnabled = true;
}
}
void OnCollisionExit(Collision collision){
if(collision.collider.CompareTag("terrain")){
jumpEnabled = false;
}
}
public void jump(float multiplier){
rigidbody.AddForce(new Vector3(0,jumpStrenght*multiplier,0));
}
}
Try putting a Debug.Log() in the jump function to see if the function is called or not. This will help diagnose the problem.
I did it and the jump function is not always called when I press the jump button, when the player touches the ground the jumpEnabled variable should change to true, but it doesn't always work
Answer by arda_k · Aug 12, 2013 at 08:27 PM
Hi, I had same issue and found this: http://answers.unity3d.com/questions/13104/why-is-getkeydown-up-behaving-erratically-only-res.html
read the answer, it explains properly.I hope it will help.