Preventing character from falling off ledge

I’m using a character attached to a CharacterController in a 3rd person game, and I need to prevent them from falling off ledges by treating any sufficiently high ledge as a wall. Using invisible blocks for this isn’t going to work- I need to code something.

My first instinct was to do a raycast directly down, and have the player snap back to their previous position if it doesn’t hit anything. But that would be incredibly ugly- the player couldn’t slide along it as they would a wall, among other things.

probably your 3d character is overlapping with the ground/ledge try to put it above a little, even the slightest overlap of the 3d cone and the ground/ledge would make it fall