- Home /
Raycasting bug when checking if player is grounded
Greetings. I have the following problem. I am casting a ray to check if the player is grounded and it works properly. When I press space I jump and set isJumping flag to true and when I land to false. The problem is that sometimes when I jump the isJumping flag is set to false because the raycast still gives that the player is grounded. It is some kind of a race condition maybe since it happens 1 out of 10 times but is really annoying. My ray has a fixed length and I have set the layermask as well.
Answer by theterrificjd · Dec 25, 2017 at 09:05 AM
My recommendation is to move the raycast into its own function that returns a bool. Call this everytime you need to.
public bool IsGrounded(){
//layermask stuff
return Physics.Raycast(......);
}
This way, you can add the if(IsGrounded()) before you set your isJumping to true. Next, OnCollisionEnter, add something like:
if(IsGrounded(){
isJumping = false;
}
To ensure that your raycast is reset consistently when grounded.
My long term recommendation is to actually get rid of the isJumping variable entirely if you can, unless you need it for something else.
Answer by thenephalem · Dec 25, 2017 at 09:39 AM
But how can I set the landing animation if I do not check whether the player is grounded every few frames? In my game the player can run on walls and make huge jumps. I need to detect whether a player is jumping or has simply fallen of a wall. Maybe i should try to use like you suggested to surround each walkable object with a trigger and to check if the player is grounded only after they leave the trigger zone.
$$anonymous$$y thinking was that if the player collides with anything (OnCollisionEnter), it will check if IsGrounded(), and then set isJumping to false.
Then you can play a game of bools, as I call it:
// Add this up where you declared global variables (isJumping)
private bool previouslyJumping;
// Add this somewhere in an Update/Coroutine somewhere
bool hasChanged = false;
if (previouslyJumping != isJumping) {
hasChanged = true;
}
if (hasChanged && isJumping) {
// Just jumped this frame
} else if (hasChanged && !isJumping) {
// Just landed this frame
}
$$anonymous$$issed the piece about running on walls.
You can accomplish this with multiple versions of the same IsGrounded(), but IsWalled() or something like that, returning an integer (I usually do -1, 0, 1, 2) or something, depending on whether a raycast is hitting neither (0), right (1), left (-1), or both (2). I choose those numbers for Axis purposes (ex: resulting in Vector3.right * 1 OR -1). Perfect in combination with a switch statement.
Another thing I like to do is, if you are using colliders, ins$$anonymous$$d of using the Tag system (which is restrictive because it only allows you to have one), you can just add a script the the wall or collider named 'Climbable' or whatever, set any unique parameters you may dream of, and then add something like:
OnCollisionEnter(Collider other){
climbable = GetComponent<Climbable>();
if(climbable){
//Your player is touching a climbable wall.
// other.normal = the direction the 'face' is facing
//applying a small force in the opposite direction of normals clings to walls pretty nicely. especially if you apply the gravity on your own.
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612145204im_/https://answers.unity.com/themes/thub/images/avi.jpg)