The player ball must jump immediately -

I have created a ball game where the player ball must necessarily jump immediately , but with this script the ball after the first jump , jump only after 4 seconds , i need that the player ball must jump immediately , how can I fix this script?


#pragma strict
var jumpObject : Rigidbody;
var jumpDelay : boolean;
var canJump : boolean;
var groundTag : String = "Ground";
var jumpHeight = 10.0;
var doublejumpHeight = 7.5;
private var doubleJump : boolean;

function OnCollisionEnter(col: Collision) {
if (col.gameObject.tag == groundTag) {
doubleJump = false;
canJump = true;
}
}

function OnCollisionExit(col: Collision) {
if (col.gameObject.tag == groundTag) {
canJump = false;
}
}

function Update () {
if(canJump == true && Input.GetKeyDown(KeyCode.Space)) {
jumpObject.velocity.y = jumpHeight;
doubleJump = true;
}
if(canJump == false && doubleJump == true && Input.GetKeyDown(KeyCode.Space)) {
jumpObject.velocity.y = doublejumpHeight;
doubleJump = false;
}
}

Have you tried logging the changes in the value of your canJump variable ? maybe the physics engine makes your ball perform “little bounces” near to the floor that prevent it to be detected as being on the ground ?

And also, while is is not directly related to the question, and if you allow me to make this kind of side comment: the way you name your fields is quite confusing; jumpHeight for example is actually a speed. canJump and doubleJump both have the same apparent semantics of indicating whever the player can (double-)jump but only one of them has a name starting with “can”. None of them is commented. I’m not telling you how you should write your own code, but I saw more than once in my own experience that this kind of imprecision leads to big trouble when you try to debug or read again your code weeks or months later.