Need help designing pathfinding NPC logic

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Pathfinding NPC that doesn’t have tons of wierd edge cases or loopholes that cause it to act unrealistically.
  2. What is the issue? Include screenshots / videos if possible!
    Can’t make one that covers all of the above.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looked on the developer hub but all I can find is tips on optimizing pathfinding/help designing very specific pathfinding NPC behaviors. I started by making a simple script that pathfinds to the player constantly, but due to how I set up my loop it would pathfind to where the player was at the time of calculation, not at the time of movement, causing them to constantly walk to where the player WAS, and not IS. Then I tried setting up a raycast that lets the NPC do a simple beeline follow if the player is within line of sight, but this caused many edge cases such as: Going out of sight for one second will cause the NPC to pathfind to you, but if you approach it while its pathfinding it won’t care and will continue to relentlessly march towards where the path is calculated to go to, and I couldn’t figure out an easy way to break a loop that was already running from outside the loop, one suggestion was to use a variable that would be true when the loop needed to break, and the loop would notice that and go false and break when it notices that it is true, this worked if I implemented non-precise wait times and whatnot, but overall felt like a hacky solution, not to mention on occasion interfering with the loop that is created right after the first loop was set to break, often causing the second loop to break before the first. Another issue was that the NPC would beeline across gaps and get itself stuck because it couldn’t easily tell if it needed to continue pathfinding or stop what it was doing to go beeline towards the player in its line of sight, this was post-loop break implementation, I ended up making a simple sub-loop that quickly creates 10 raycasts that sample interpolated points along the line of sight and raycasting downward to detect pitfalls, but this ended up creating even more issues in the long run, is there a more seamless and simple way to do this??
1 Like