Making my player fall to the ground when they are colliding with an object?

A solution I’ve tried to prevent my character controller from being stuck on the edges of objects is two have two simple Raycasts, one (Raycast1) that shoots forward from the players feet and the other (Raycast2) which shoots downwards directly under the player.

How this is supposed to work is if Raycast1 detects an object, but Raycast2 does not detect ground below the player, I get “pushed” back by using controller.Move(-transform.forward * Time.deltaTime * 2f);. This code works great on cube and similar type objects and the “push” is not noticeable at all which is very good, but on round objects it is very inconsistent, sometimes it works and sometimes it doesn’t. Adjusting cast distance did not help and neither did adjusting Skin Width.

void Update()
{
    isGrounded = controller.isGrounded;

    ...

    if (Physics.Raycast(transform.position + transform.up * -0.8f, transform.forward, 0.5f)) //Raycast1
    {
        if (!Physics.Raycast(transform.position + transform.up * -0.8f, Vector3.down, 0.3f)) //Raycast2
        {
            controller.Move(-transform.forward * Time.deltaTime * 2f);
        }
    }
}

fu5g4s

Can I assume that within the elipsis above (…) you are ALSO calling the .Move() method?

If so, that is bad. It will invalidate ground checks, causing all kinds of mayhem. Fix that first. Here’s how:

I wrote about this before: the Unity example code in the API no longer jumps reliably.

If you call .Move() twice in one single frame, the grounded check may fail.

I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

Here is a work-around:

I recommend you also go to that same documentation page and ALSO report that the code is broken.

When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!