10 Replies

You could do a short Raycast in the down direction to check if the ground is there. "short" in this case means the distance from the player pivot to the ground (distToGround); in most cases, collider.bounds.extents.y is this distance (unless collider.bounds.center isn't 0,0,0). It's advisable to add a small margin (say, 0.1) to compensate for small ground irregularities or inclination:

Aldo Naletto's idea's really good, I've been using this in my game for about 2 months.
But there's one small flaw. If the ground is a bit uneven, it doesn't really work. "I'll cast the ray further then" was my first idea, but what if it's a hole?
What is needed is a capsulecast instead of a raycast:

//collider.bounds are the bounds collider relative to the world. I wanted a 0.1 margin, and 0.18 is the radius of my collider.
Physics.CheckCapsule(collider.bounds.center,new Vector3(collider.bounds.center.x,collider.bounds.min.y-0.1f,collider.bounds.center.z),0.18f));

I tried this but I found it too unreliable. Even if I set the sleeping mode to "Never sleep" OnCollisionExit sometimes fails to fire for some reason (I think). This causes my player to suddenly learn to fly...

My script was a bit modified, though. Instead of a boolean used a counter to keep track of how many objects of the "floor" type the player is colliding with (I have multiple platforms instead of just one "floor").

In my game, using the OnCollisionEnter/ OnCollisionExit approach led to an annoying behavior where if the collision detection box for the ground check left the space of one ground object while still in the space of another ground object (imagine the border between two flush rectangular platforms) it would activate the OnCollisionExit method, despite my character being grounded. I am unsure how to avoid this behavior, so I will be using the Physics.CheckCapsule approach instead.

Attach the script above to your character (your character has CapsuleCollider component), then run the game, look at your character in Scene widow instead of Game Window (because Debug.DrawRay only works in Scene window), you should see a white ray where the character touches things.

I tried to translate it right off but that didnt help. I am new to java and c# so please dont flame me.

I did this but get some errors like:

Operator &&' cannot be applied to operands of type bool' and UnityEngine.Vector3' The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments

in C#, you can change a double (0.5) into a float by adding f.
so you want to change your line
return Physics.Raycast(transform.position, - Vector3.up, distToGround + 0.1);
into
return Physics.Raycast(transform.position, - Vector3.up, distToGround + 0.1f);

Also, your IsGrounded method returns a vector3, it should return a bool.
Finally, I'm sorry, but you'll have to stop posting your problems on a non-relative thread.
I also advise you to search for your errors before posting on a forum, because you'll lose a lot of time if you ask for help every time you get a cast error.