1
\$\begingroup\$

I would like to make an enemy walk randomly around a map I have made and if the player is in his zone of sight the enemy to change state to attacking/following. So how would I accomplish something like this? Currently I have no idea how I would generate random paths with collision avoidance.

EDIT: I am making a 3D Game.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

You may be interested in Nodal Pathfinding. The concept is similar to placing waypoints, except you generate these way points on top of your world. The waypoints are then used with an A* pathfinder, to determine the optimal path.

Have a look at the blog here:

http://www.jgallant.com/nodal-pathfinding-in-unity-2d-with-a-in-non-grid-based-games/

Another option, is to use Navigation Meshes. Unfortunately, you cannot use the Unity NavMesh, as it does not work in 2d. You either have to buy one from the Asset store, or create one yourself.

\$\endgroup\$
1
  • \$\begingroup\$ I dont think he has specified that it is 2D, without specification of dimensions, the navigation meshes are still an option \$\endgroup\$
    – nickson104
    Aug 26, 2015 at 11:13
1
\$\begingroup\$

Use waypoints in your map.

  1. The enemy AI in "patrol" state chose a random visible waypoint as target.

  2. Reached the waypoint repeat step 1. (eventualy excluding current waypoint)

If you don't want to place manualy waypoints you can generate them at the corners via script : here follow an example:

enter image description here

cons: the enemy will patrol in straight lines from waypoint to waypoint. pros: the patrol state grants walls collision avoidance

\$\endgroup\$
0
\$\begingroup\$

Pick a random point on the map which is not blocked. Then Calculate a path from the current position to the destination using a stock route finding algorithm like Dijkstra's Algorithm or A*. When no route can be found, then the random destination is either unreachable. Pick another point in that case.

To avoid a deadlock in case there is no possible move the actor can make, limit the maximum number of attempts to find a destination. When none is found, just let the actor stay where it is.

Every game logic frame, have the AI actor perform another step along the calculated path. When it reached the destination, pick a new random destination as explained above.

\$\endgroup\$

You must log in to answer this question.

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