Which do you think is the best method to check if you are on the ground and ready to jump? Right now, I’m doing the following:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "terrain")
{
IsGrounded = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "terrain")
{
IsGrounded = false;
}
}
This works great on Unity terrain, but I have a ton of floor tile prefabs for my dungeon, and it’s easy to miss one and not tag it “terrain.” Plus, it’s a pain to tag them all. Even when I do tag all of the colliders, sometimes he jumps, and sometimes he doesn’t. As an alternative, I tried the raycast method in the following post:
I played around with the distance to raycast for a while, but it was never perfect, and spamming the jump key leads to premature jumps too often.
I’m curious how others handle this – collision, raycast, or some other method. Some examples would be great.
I , too, “struggled” with this for a long while… reading various posts, answers, ideas, solutions, etc…
Currently, on a side “fun” project that I play around with from time to time, I have my character setup with an additional small box by his/her “feet”. The box isn’t very big, but big enough to hit anything directly under the player.
That collisions between that little box and my character’s collider ignore each other.
Then, I have an integer variable that keeps track of how many colliders I’m in contact with, via OnTriggerEnter/Exit (the little box is the trigger*).
This allows me to check when I can jump, and when I do, I set a variable for jumping, do animation, apply force, etc…
and only when I’m not on the ground or already jumping , I’m allowed.
Also, it tells me when I’m falling, since if I leave all contacts(triggers count = 0) and I’m not jumping, I must be falling…
I have no idea if this is the best, but for me it seems to be working well in my experience thus far.
In the past, I’ve offered simpler advice, and sometimes it depends on the scene/setup… someone may not need as much ‘detail’ ?
Maybe this will help ya 
If you find a better/cooler solution, it’d be cool to hear what you decided to use, too… I’m always open for suggestions 
Thanks for the suggestion. I modified your method slightly, and it works great in all situations I’ve tested, even complex topology. I made a separate game object attached to the player which has it’s own tiny box collider at the feet, and it’s set as a trigger. Then, I used the following bit of code to make it work:
private void OnTriggerStay(Collider other)
{
IsGrounded = true;
}
private void OnTriggerExit(Collider other)
{
IsGrounded = false;
}
Since I use OnTriggerStay instead of OnTriggerEnter, it works even if the player walks from one ground collider to another.
Cool.
Separate game object was an option, too. Since my colliders were different types, I just put it on the character, but no real difference.
The stay was an option for me, too, but that’s why I kept count of mine, as I mentioned…because otherwise it messed up slightly walking from one to another 
In the end, though, I’m glad it worked for you
Hopefully it continues to do so lol
Yeah, I hope I don’t break it. I put it on a separate layer from the player, so no problem there. I may make the box a sphere though, so I can set it to not jump if it collides at a bad angle, like with a wall or something. That was the advantage with the terrain tag.
I thought about that with mine, and though admittedly, I haven’t done a ton of testing, my box is not big enough to hit the wall, I think… anyways, of all the issues, hopefully that is minor 
My player character has a wide stance, so I had to make the box a little large.
Fair enough. I guess by “small” I meant it wasn’t as wide/deep as the player. Just a little less 