Hi, what are some good ways to check if grounded? I need the best way possible. I tried:
-Raycasting
Cons: Only works if center of feet is on the ground
-Trigger collider under feet
Cons: Interferes with other player colliders and sometimes doesn’t work
-Checking velocity
Cons: Doesn’t always work, due to variety in velocity
I too tried numerous methods and ended up using a SphereCast. It takes some tweaking to get it right, but it has unquestionably been the most reliable.
In a simple form, it would be:
var grounded = Physics.SphereCast(new Ray(checkOrigin, Vector3.down), checkRadius, checkDistance);
Hope that helps!
A CharacterController has a isGrounded function, I use it for my player in my project after
[RequireComponent (typeof (CharacterController))] at the start of the script
Controller = GetComponent (); run once
if (Controller.isGrounded) I use to check if it is well…grounded
Any of those ways can be used, depends on situation.
I’d like to admit that adjusting layers can solve your problem with collider under feet, just put environment objects in one layer, your trigger in another one and set your trigger’s layer to interact only with environment. Take a look on Layer-Based Collision Detection page.
Checking velocity is not an option, I guess, so you’d better refrain from doing so. It will work only in very particular cases.
And there are two ways you did not mentioned:
Hope that helps.