- Home /
Super basic enemy pathfinding AI
So far I have a simple test scene for an RPG game with a player in the center and enemies spawning around him. The player and the enemies are using Character Controller component in order to control how tight they can bunch together. The enemies have a script which makes them move towards the player. When the player starts running around all enemies start following and they tend to form a tight crowd and everything is fine so far.
The problem comes when the player stops and the enemies that are bunched together need to surround him. To do that, they have to stop following the straight line path and move a little bit around in order to find space around the player.
I can think of a number of solutions, one of which uses colliders or ray casting in order to find out if the path to the player is obstructed, but I have the feeling it's not a very elegant solution. I just wanted to hear some other opinions, I'm sure there is a simpler way/algorithm/logic to make the enemies surround the player.
I'm not really expecting the enemies to be super clever, I want to keep it light and simple, because I'm targeting mobile devices. Any ideas are appreciated!
Answer by Velo222 · Oct 27, 2013 at 05:12 PM
Hey just throwing out a couple ideas here. I'm currently working on an RTS and in order for my units to "attack" an enemy, they have to surround the enemy as well. The way I'm doing it currently, is I'm using Aron Granberg's A* pathfinding, and when a unit gets a certain distance from the enemy (I keep track of the distance to the enemy target about every .5 seconds) the unit then stops and updates the grid beneath its collider or bounds to be "unwalkable".
Then I also have my units calling a path every .2-.6 seconds while they're attacking, and if the pathfinder sees that a grid cell is unwalkable, it paths around it. Thus, my units path/steer around units that are "attacking" the enemy already. The downside to this is that the grid cells are not fine-grain in resolution, so I get some units "sliding" around units on some occasions -- as the path isn't as precise as I'd like it to be for performance reasons.
The other idea I was thinking of, is to store say 4-5 points around your unit (maybe even use an empty game object), and have your units go directly to those points. Then when one point gets "filled", mark it as taken, so the other units will only try to fill the points that are left. I'm not quite sure how you could do this, but maybe using raycasts/colliders/local coordinates etc it might be possible.
Overall good luck and I hope you find a solution.
Thanks for your answer. Actually before I ask this question I tried out A* and it seemed way too complicated for my needs and I couldn't get it to work properly. Then I did some research, watched a few youtube videos to see what it can do and it seems like it's the best choice for RTS and TD kind of games. So I've been playing with it for the past 6 hours or so and the result that I'm getting is encouraging.
It turns out the best way to manage big crowds is to use the Alternative Paths modifier script (comes with A*). Just put it on the enemies and don't do anything with the grid.
I will definitely spend a couple of days playing with this cool library.