- Home /
Problems with jumping script.
Okay, so I have two scripts. One that is checking if the player has collided with a specific type of object that will act as a switch for the script that will add a force to my player to make him jump, the problem is that I can jump multiple times even though I can't seem to understand why. Here are the scripts:
The "switch" script:
// this is restricting or allowing the player to jump, a jump switch.
function OnCollisionEnter(gndCollision : Collision) {
if (gndCollision.gameObject.tag == "ground" || "jumpableObject" ) {
playerJump.isGrounded = true;
Debug.Log("grounded!");
}
else{
playerJump.isGrounded = false;
Debug.Log("not grounded!");
}
}
and the jumping script:
// this is where the jumping happens
static var isGrounded : boolean = true;
var jumpForce : float = 50.0;
function Update () {
if(Input.GetButtonDown("Jump") && (isGrounded == true)){
rigidbody.AddRelativeForce (0,jumpForce,0);
}
}
Also, the jumping part seems to work out, but it is the part where the "switch script" will return false that is the problem as it does not return false to the jump script.
Thanks in advance.
Answer by robertbu · Mar 25, 2013 at 02:36 AM
OnCollisionEnter() only gets called for a single frame when a collision first starts, so you only have a single frame in which you can jump. Try using OnCollisionStay() instead. Also your 'i'f condition is wrong. It should be:
if (gndCollision.gameObject.tag == "ground" || gndCollision.gameObject.tag == "jumpableObject"
Thanks, this solved half of my problem and answered some questions I have had many times before regarding collision. But for some reason my player can double jump now. hmm, I'll check into it and mark this as answered, thanks again!