Hi everybody!
I’m having problems while working with CharacterController at making it jumping in my platform game. I made it so, when you press the Jump button, it set the “onAir” bool to TRUE, and set the vertical speed needed to make the controller jump. So, while the character is on air and falling, I want it to check if is touching the ground while is on air, making the “onAir” bool FALSE. Unfortunately, “isGrounded” doesn’t work for checking if the controller touch the ground while falling, so the “onAir” stays always TRUE, and the controller can’t jump again. I don’t want to set the “onAir = false” while “onGrounded”, because sometimes, the controller stays on ground (replacing “GetButtonDown” with “GetButton” evades this problem, but it works like doing the jump TWO/THREE times). The thing I need, is a method that makes the controller check if is touching the ground while falling.
Here is my code:
if ( controller.isGrounded ){
if (vertSpeed <= -5)
vertSpeed = -5;
if ( Input.GetButtonDown("Jump") && !onAir ){
Debug.Log ("IS JUMP");
jmpTime = 0.2f;
onAir = true;
vertSpeed = jumpStrength;
}
}
if ( !controller.isGrounded ){
jmpTime -= Time.deltaTime;
if ( controller.isGrounded && vertSpeed < 0f && jmpTime <= 0f){
onAir = false;
}
}