Problem with being Grounded

Hey,
I’m trying to find out whether my Player is grounded or not. To achieve this I tryed using the following code in script:

if (Physics.Raycast(transform.position, Vector3.down, 0.2f)) Grounded = true;
else Grounded = false;

I’m building a 3D sidescroller, so my charackter starts with a rotation of [0, 90, 0]. Now if I’m rotating my player around the Y axis, the code above somehow stops working.

If I turn my charackter 180 degrees around the Y axis ( [0, -90, 0] ) I have to switch the part of my code wich says “Vector3.down” to be “Vector3.up”. However this fix didn’t pass stress testing and it will still fail sometimes. And when it fails, It fails for the rest of the level.
My charackter rotates himself, when I switch walking directions. And inside that rotation (I guess about 90 degrees in) I also get Grounded to be false for a short amount of time.

I have no Idea why this isn’t working. Why do I need to change the Ray rotation? Can you guys help me out, please?

For some reason, my character is entering the ground, when it’s rotating. I was able to sort of fix this by adjusting the Ray Position slightly upwards.

Vector3 rayP = new Vector3(transform.position.x, transform.position.y + 0.01f, transform.position.z);
if (Physics.Raycast(rayP, Vector3.down, 0.2f)) Grounded = true;
else Grounded = false;

Atatch a character controller then make a Controller variable and give it a name like “cont” then do this:

if (cont.isGrounded) {
doStuff();
}

Try using transform.Down instead of Vector3.Down being the first a local space vector and the second world space.