# Send an enemy back to its spawn point using waypoints

Hi, so far my enemy is walking towards the player when the player is in a certain range of the enemy. When the enemy reaches meleeAttackrange it attacks and when the player manages to get out of the enemies deception radius - the enemy runs back to its spawn point. Here is where I have a problem.
So far the enemy chooses the shortest way back to its spawn, which is quiet a problem since my level is a dungeon. The enemy runs against the closest wall …
My idea was to send the enemy back to its spawnpoint using waypoints. I have placed some lights in my map on strategic points and attached an empty gameobject (waypoints) to those points. The plan is to make the enemy search for the closest waypoint and walk to that waypoint and then decide wether it needs another waypoint to reach the spawnpoint or wether it can reach the spawnpoint without walking to another waypoint.

So far I have the following idea:

create a function called findwaypoint.

in this function i am checking the distance of the enemy.position to the waypoint position closest to the enemy. If walking to this waypoint shortens the distance between the enemy and the spawnpoint - walk() is called and then check again until the enemy reaches the spawn point.

There is a flaw in my logic. and i still cant figure out how to manage…
the shortest distance from the enemy to the spawn is the straight line and walking to the right waypoint might enlengthen this distance. Further, right now this idea may choose a waypoint that leads to nowhere…

unfortunately i am not sure how to make a walk home using waypoint function.
Does someone have an idea?

Consider using the path finding system in Unity 3.5 (Requires Unity Pro) or other third party path finders.

If you want to roll your own code, maybe you should start with an A* solver. Also, I would treat a no line of sight to target as a bad path and ignore that all together, so you might want to ignore all possibilities that clearly lead nowhere.

Hi, thank you for your answer. It might take some time until I will be able to fully understand path finding systems the way I need them to work

I admit, I am new to unity and at the moment I simply cannot afford the pro version and I don’t want to download the pro version illegally. However, it is on my wishlist