Character controller check ground while falling.

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;
			}
		}

My first comment is that onAir and isGrounded should not be true at the same time. Setting onAir to false when isGrounded is true is the right thing to do. If onAir is supposed to remain true when your controller is on the ground to prevent it to jump again then either you need to rename the variable (“hasJumpedOnce”?) or you need another logic to prevent your character from jumping again.

Now, if you want to stick to this, you can do a raycast towards the ground to check if your controller is close to / on the ground. That is basically what your controller is doing to update “isGrounded” though.

If I am missing something please explain because I am a bit confused by what you are trying to achieve here.