I had wrote a reply to an old thread but then I found a better solution. There is a lot of answers to this question if you search - they usually involved using some vector math to basically get opposite direction from player and then find some point to move to that way.
I found this to be lacking once you get to actual implementation, and also harder to tweak especially if you are not strong in the vector maths.
Here is a better solution IMO:
The basic idea is to create a grid overhead of the actor that will flee.
- Shoot a line trace down from each point of the grid.
- Put those hit locations into an array.
- Also get the distance from each hit location to the player (or whoever you are fleeing from) and add that result into a float array.
- Get the max from the float array and use the index to get the associative location in the location array.
Result (pink sphere is showing the furthest away point)
Code:
1.
2.
3.
You’ve now identified the furthest location from the player, which you can use to tell the AI Controller to move to.
Obviously you can create further considerations from this - you might get the path to each location and weight it based on various criteria, or favor locations lower or higher than others, etc etc. But the main idea is to use this grid rather than vector math, as it gives you an easier time to tweak parameters and also you don’t have problems so much with ensuring you’ve chosen a valid location.
Note that I got this basic idea from some stuff you can do with EQS and blackboard - in this case I am working outside of that for simplicities sake, but thats where the idea came from.