What's the best way to keep an object from falling off of something?

I have an enemy that charges at the player (the game’s 2D by the way) and the code I’m currently using to keep enemies from walking off platforms isn’t working because they’re going too fast. So what is a good way to keep a fast moving enemy from running off something.

invisible colliders