Collision detection for a First-Person Horror Game

I am having issues with detecting collision in my game. My method goes like this:

In OnCollisionEnter() I test all contact points for collision with the object. If the collision angle is less than maxSlope, the collision is with a floor and the character sets a boolean check of grounded = true. If the collision occurs with an object higher than the max slope, the object is not valid for a floor and the character is ungrounded. also, if the character jumps, he is obviously ungrounded. In OnCollisionExit() i set grounded = false always. The problem is that if my character comes to a wall and runs into it, he becomes ungrounded, and he never becomes grounded again. Does anyone know a better method of detecting collision with this scenario? I can provide my scripts as well if they are needed.

If you’re using a first person controller, they have a boolean built in that detects if you’re grounded or not.

I believe that’s built into the character motor.

If you’re using a rigidbody for your controller, the best way to detect if your character is “grounded” is to simply cast a ray downwards, with a very small distance. Like so:

grounded = Physics.Raycast(transform.position, -Vector3.up, distToGround + .1f);

DistToGround is the length of your model. You can do this by checking the bounds of your collider:

distToGround = collider.bounds.extents.y;

If a ray doesn’t reach from your object to the bottom of your object and collide with ground, then you’re airborne! Hence the distToGround + .1f (adding just a tiny bit of space).

I found my issue. I changed OnCollisionEnter to OnCollisionStay to more accurately detect standing collisions.