Simplistic AI Pathing

Hi everyone,

I’m currently working on a learning project and I’m trying to get some ideas around how to implement a pathing system for AI that is as simple as possible.

About the project:

  • It’s a top down game using Unity 2d (x/y)
  • The level is built with sprites and 2d colliders
  • I want to have a lot of “AI” running at the same time (Think Diablo style zerging of monsters)

I’ve built a priority system that sets the “target” for the AI to move towards but I’m having trouble finding any information on how to make AI path around walls and to a target. I’ve looking into some of the A* Pathing but it feels very complex and I was getting crazy performance issues, etc with large levels and I’m hoping there is a simple solution (raycasting and move towards? Not sure…)

Note that I’m relatively new to Unity and I’m learning, so more details you can provide would be very helpful.

I’m looking for ideas/support on how one might do this, I am not asking for someone to write it for me (Although I won’t hate some example ;))

Thanks!

So I’ve tried a few different things (different A* implementations, raycasting, etc) but I haven’t been able to get anything to work in the way I’m hoping.

You don’t need running A* for all enemies in you project. Set some rules, for example:

  • enemies have radius around where they are see the player
  • calculate A* only for enemies who are now visible on the screen
  • for enemies behind screen set some patrolling path

Sorry, I’m not sure what you mean. Is there a tutorial on how to setup A* for 2D? I haven’t been able to find any. I was able to get it setup, but I don’t understand how to:
A. make it work with a big map (I tried making it huge and it lags like crazy)
B. follow a moving target, I was able to get the AI to move to a target but not move WITH the target.

In an attempt to do write something myself, I do the following:

  • Cast a ray in front of the AI

  • Cast a circle ray around the AI

  • Cast a smaller circle ray around the AI

  • If the player hits the front ray, the AI will move towards them

  • If the player hits the circle ray, the AI will turn towards them and when the front ray hits them move towards them

  • If the player moves out of the front ray, the AI will pull its last “seen” location and start moving there, stopping after a second or two.

  • If the AI is not chasing the player and the smaller circle ray hits a wall, the AI will move slightly away from the wall.

It seems to work ok, they can move around walls and don’t seem to get stuck but I’d really like to be able to do a more “find a target and zerg it” style of AI vs. raycasting.

Does it make sense to just drop 2D and use X/Z with the built in Nav Mesh for pathfinding? Zerg style AI is a big portion of the game so I need to figure out a good way to make it work.

Again note that I’m still learning! :slight_smile:

This book will help you “Unity 4.x Game AI Programming” by Aung Sithu Kyaw, there are many patterns.

How about making a List of waypoints with a vector3 position and whenever the AI runs back just send it to waypoints[0]?

Not really sure waypoints will work. To give you a better idea of what I’m thinking/trying to do:

  • There will be 4 “characters”
  • The player can switch between them.
  • Throughout the map, I want to have spawn points for AI
  • Once an AI spawns, I want it to check the distance between itself and the four characters.
  • Assuming it finds a character (or multiple characters) within its “max range”, it will select the closest character as its “target”
  • From there, I just want it to zerg that target (I.E. move from where it is, to where the target is). Noting that the target may or may not be moving.

I’ve got the picking the target piece down fine. Problem is I can’t seem to figure out a “fast” way to do the zerg path finding. The levels won’t be very complex, but they will be fairly large.