I was trying to make a script to make my enemy follow my player intelligently (but not too much) and I want to know if there was a better way to do it (of course there is but I wanted to find a way myself) Here is what I did: I wanted my enemy to detect the player when he approach a certain distance from him. I have a varibale to determine what distance the nemy have to back up (when player is too close) and when it has to follow the player (player too far). Now, I didn't wanted the enemy to just forgot there is an intruder when the player goes around a corner and they can't see him. When the enemy sees the player directly (raycast), they rotate toward him and start following him directly by normalizing the difference of their 2 position. Now is the tricky part i'm not sure about. My player has a list that keeps the 1000 last positions of my player and that update every 0.01 second to add the lastest position and remove the oldest. If the enemy can't see the player directly (!raycast) he goes through all the positions in the list by checking for every Vector3:

1-is there something between the enemy and this position ?

2-is there something between this position and the player ?

Then it add up the distance between the enemy and the point and the distance between the point and the player and checks

3-is the lenght of the path by going at this point shorter than the last shortest path ? if it's smaller, it's assigne to the best point.

If it detected a point that matches these conditions, it goes at it and if it doesn't see the player, it continues checking for new points. If no point matches the condition, he just stop following the player (the enemy lost him). All this works with raycasting to check positions, presence of objects between enemy, player and points and path to go.

  • 1
    \$\begingroup\$ Is there a specific problem that you've observed with your current method that we can help you solve? \$\endgroup\$
    – DMGregory
    Apr 8, 2020 at 14:39
  • \$\begingroup\$ Well, I was thinking that my method was maybe way too complicated and that there may be some easier solution. \$\endgroup\$ Apr 8, 2020 at 14:58
  • \$\begingroup\$ The easiest thing is to not write new code, and continue using what you have that works. Any other solution is necessarily additional work. ;) If what you have is giving you the gameplay you want, with adequate performance and maintainability for your needs, then you might as well stick with it until it gives you trouble. \$\endgroup\$
    – DMGregory
    Apr 8, 2020 at 16:00

1 Answer 1


You can make the enemy search for the path to the last point where he saw the player for the last time, and search a bit further after that along the vector where player has disappeared, but you need to save also some points after the enemy lost the player. Or you can get the vector between last and previous points for this.

Also, if you want to check all points, you can do a binary search for the best point.

And make sure to add only points that exceeds certain distance from the last saved point.

  • 2
    \$\begingroup\$ Since this is in Unity, using a navmesh (which has its own built-in pathfinding search) may be easier than applying A* manually. \$\endgroup\$
    – DMGregory
    Apr 8, 2020 at 16:57
  • \$\begingroup\$ @DMGregory My bad, I corrected my answer. \$\endgroup\$ Apr 8, 2020 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .