Make an NPC run away with pathfinding?

I’ve searched for a solution and only found 1 other topic without a solution.
How could I make an NPC run away from a player but with pathfinding so they dont get stuck?

My best solution right now is to have a manual pathfind by raycasting in 8 directions around the character finding the point that has an opening and goes the farthest from the character the NPC is fleeing from. I would prefer actual pathfinding though.

tbh what you have made already sounds pretty good. If the NPC initiates this for a fixed period of time or a specific distance, then constantly update the raycasts to detect obstacles and the NPC will have a reasonable behaviour.

I guess the question from me is why do you want the NPC to run away?

1 Like

mechanic in my game where if you hit NPCs they will scream and run, and if you continue hitting them they will then fight back. The issue with my solution is that its not as perfect as pathfinding. I could pathfind to the raycasted position itself to improve it but its still not perfect.

(e.g if the NPC is in a room with 1 door exit, he will be running around the room in random directions until he finds the exit rather than running straight for the exit)

If you add a tag to the door exit, then you could pathfind to the nearest exit point. tbh seeing the NPC running aimlessly round the room sounds funny to me.

2 Likes

that would be tedious though, its an open world game with lots of places. Also there may be similar places that are not rooms.

hmm… I don’t see how you could otherwise let the ai know that there exists “exits” without previous declaration of exits. Plus keeping track of whether or not they are in a room.

If you don’t want it to inherently be able to get stuck, use pathfinding:computeasync?

1 Like