- Home /
NPC Fleeing with Unity Pathfinding?
What is the best way to make enemies flee from your character. I need to create almost an impulse where the player causes the enemies to run away from him in all directions. I have tried a few things, one of which was additivly altering their destination but this didnt really have the effect i wanted.
Any tips, maths or links to information is appreciated.
Kind Regards
Brian
Answer by Owen-Reynolds · Jun 02, 2013 at 09:41 PM
Pathfinding and fleeing in terror usually don't mix. Fleeing might be running down a hallway and turning into a dead-end. Even if you know about the dead-end, still an OK place to hide (and you might forget in your panic.) Pathfinding will never have you run into a dead-end.
With pathfinding, you first pick a spot. Suppose you pick just past the end of the hallway. The pathfinder figures out you have to run crosswise to the scary thing in order to get to there. It ends up looking more like "everyone and calmly follow the exit signs" Using pathfinding + avoidance will probably prevent you from "fleeing" towards an enemy standing in the only doorway out of a room, but it can't help you know what to do in that situation, or where to try to flee to in the first place.
Simplest trick is to do a few quick ray casts "away" from the scary thing, and run whichever one has the most room.
That is probably a little more sophisticated than I need and probably a little expensive considering all I really need is for the NPC to run in the opposite direction to the player. You did give me a clue though.
I think I may have to bust out some pythagoras theory. Work out the direction vector and just get them to run along it if a forward facing ray doesnt have a collision too close otherwise pick another direction to run in.
It just feels a little clunky to be honest. There must be a standard fleeing algorithm out there.
The run direction is just me.position - scrayThing.position;
. Same as chasing someone, but negative.
There isn't really a standard way that things should flee, which means there's no standard flee code.
Its ok I sorted it but as you gave me the idea, heres one internet :) Have a nice day.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103415im_/https://answers.unity.com/themes/thub/images/avi.jpg)