Restricting a character's movement at a ledge?

Recently, I have been having a problem where the enemy I’m currently working on, comes to the end of a ledge while patrolling back and forth, and then continues to walk off the ledge for a little bit before turning around and heading back in the opposite direction, resulting in the enemy briefly walking on air. How do I keep from happening? I want the enemy to turn around as soon as they reach the end of the ledge and not walk on air.

Does anyone have any ideas on how I can restrict how far the enemy can walk forward when they then reach the end of a ledge?

Oh, and just in case, I should also mention that I have Can Walk Off Ledge disabled, which is how the enemy is able to patrol and back and forth without falling off the ledge, as well as Use Flat Base for Floor Check enabled.

As always, any help would be greatly appreciated!

Can anyone help, please?

Is there anything that can be done to fix problem?