# Make enemy follow player intelligently

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.

• Is there a specific problem that you've observed with your current method that we can help you solve? Apr 8, 2020 at 14:39
• Well, I was thinking that my method was maybe way too complicated and that there may be some easier solution. Apr 8, 2020 at 14:58
• 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. Apr 8, 2020 at 16:00