- Home /
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;
}
}
I just tried your code and worked perfectly - So I'm asking you: did you attached that script to the ball or what? I'm asking because I can't find a reason to assign the ball object to a variable jumpObject if the script it is supposed to be attached to the ball. Anyhow this is your code cleaned up a little bit:
#pragma strict
var jumpDelay : boolean;
var jumpHeight = 10.0;
var doublejumpHeight = 7.5;
private var canJump : boolean;
private var groundTag : String = "Ground";
private var doubleJump : boolean;
private var jumpObject : Rigidbody;
function Start()
{
jumpObject=this.rigidbody;
}
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.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
jumpObject.velocity.y = jumpHeight;
doubleJump = true;
}
if( canJump == false && doubleJump == true && Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
jumpObject.velocity.y = doublejumpHeight;
doubleJump = false;
}
}
Answer by graslany · Nov 26, 2014 at 08:58 AM
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.
Your answer
Follow this Question
Related Questions
How to trace the movement of player animation? 0 Answers
i am trying to make my character exit the car, its almost done, i just need a help 0 Answers
my ball sometimes jumps higher or sometimes not 2 Answers
PlayerHealth drops fast and wont reset 1 Answer
why will my jump animation only play on multi-jumps? 0 Answers