- Home /
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;
}
}
Answer by GiyomuGames · Oct 16, 2015 at 01:05 AM
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.
Actually, I resolved the issue with another method, but I'm gonna explain that later.
$$anonymous$$aybe I redacted the text bad, but I didn't wanted setting "onAir = false" on every instance of "isGrounded", what I wanted is set "onAir = false" at the moment the character touches the floor.
What I did was, add a new bool "chkGround". On the "!isGrounded" section, while vertical speed is below 0, it sets "chkGround = true":
if ( !controller.isGrounded ){
if (vertSpeed < 0)
chkGround = true;
}
So, when the controller touches the ground, I put a condition inside the "isGrounded" section which checks if "chkGround" is true:
if ( controller.isGrounded ){
if (vertSpeed <= -5)
vertSpeed = -5;
//This check the ground once.
if (chkGround){
onAir = false;
chkGround = false;
}
if ( Input.GetButtonDown("Jump") && !onAir ){
Debug.Log ("IS JU$$anonymous$$P");
onAir = true;
vertSpeed = jumpStrength;
}
}
With that, the "onAir" variable sets false at once, because the "chkGround" stops working. This is useful for situations like wanting the player stops doing aereal attacks at the moment it touches ground.
Anyways, thanks for the input.
No problem, the important is that you got your problem sorted out :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612041451im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Network Client does not Groundcheck 0 Answers
Jumping not always work 2 Answers
I aren't able to jump. I find no mistake in the script 1 Answer
How to jump multiple times to mid air? 3 Answers
I cant make my Character jump. 0 Answers