What is the best way to make an AI run away from the player without it getting stuck in corners?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By even_nuller

So basically my enemy has 4 states and one of them is running away from the player but I can’t just make the enemy run in the opposite direction of the player since that would make the enemy get stuck in corners.

EDIT: I want my enemies to run behind cover and when they are detected move to another cover

:bust_in_silhouette: Reply From: Inces

And what do You want them to do instead ? If You wish them to move along the walls away from the player You can give them 2 raycasts on left and right, so whichever collides first will modify angle of current running trajectory. But they will eventually be stuck in absolute corners. You can also use those raycasts to detect unescepable corner, so You can change enemy state to some kind of desperate attack.
I used this raycast approach in my hack slash and as much as it seems to be good idea, it requires a lot of tweaking - alignment, pointing direction and position of raycasts must be thought out accoriding to objects speed and torque, otherwise your enemies will be going apeshit around walls :slight_smile: