Hi.
I’m attempting to write a complex zombie AI to be better at making AI and I’m wondering which method other people think is the best way to check if a zombie is close to a player.
Method 1:
calculate the distance to the closest player, if the distance is larger than 50. only update the distance every 5th second. if the player is closer than 30, check the distance every 2nd second and if the player is closer than 5, update 2 times each second.
This way if the closest player is far away from players then they won’t keep updating the distance to the player as often.
The problem here is that now and then I get a small lag spike if I use place more than 50 zombies into my map. when they’re all close to one of the players in the scene, they call the updateDistance function to check if they’re close enough to start hitting.
I made it so they call the function with a random interval so that all the zombies won’t call the function at the same time but still there’s lag.
Method 2:
Use sphere colliders. If the player is within a certain sphere collider the zombie will start chasing the player. if he then also enter the collider closest to the zombie the “attack Collider” or so to say then the zombie will start attacking the player.
I haven’t tried using this method because before I spend time trying to figure out how to do it correctly I want to know if this really is a “Good” method.
Method 3:
Just like method 1. except that if zombies are closer to each others, then they’ll become a group and check the distance together. so that a gameObject is created between them and it’s that gameObject that is used in calculating the distance to the player instead of calculating the distance from each individual zombie.
A good thing with this is that if zombies get closer to each other and follow each other, we have a horde which will be difficult for the players to get past.
The negative thing with this is that I’d have to write a lot of extra code to make them “Team” or how to call it.
So my question, again. Which method do you think is the most optimized one, or do you have a better one that works for larger amount of zombies without eating up the PC’s performance?
Thanks for reading 
Kind regards
-Frank
3 Answers
3
You could try a grid-based method where you divide the entire map into grids of an arbitrary size. Then each zombie is contained within the grid it inhabits, and as it passes through the boundaries of it’s current grid into the next it is transferred over to the new grid. At this point you’re able to check the grid the player currently inhabits (or grids surrounding the player if you want more realistic behavior) and find every zombie that inhabits the grid(s). This way you will only perform distance calculations on the zombies are even relatively close to the player.
It is not a simple system, but it is very efficient. Let me know if you have additional questions about this method. You should be able to find information about it online as it is a known method.
I like the grid method a lot that seems like a great way to do it, I’ll need to experiment with more stuff like that when I start doing randomization of maps which I also am thinking grid type stuff could be very helpful with that.
In my current game I have several tests to determine if the enemy will attack the player. The phases are like:
-
If the enemy is in range of Player Big Area Collider then the enemy’s first Update condition turns to true. If it’s false the Update and fixed update will immediately return, depending on how far you want zombies to patrol around at, this collider might need to be quite large.
-
Once they are inside the Player Big Area Collider it does a distance check for the enemyAttackDistance variable. I do this every frame but I really think you’re on to something with having the delays based on distance, I might try to add that later.
-
If they are within the Player Big Area Collider and the attack distance I finally do a line cast to see if they have line of sight of the player.
If they pass all 3 of those conditions they finally pursue and attack the player.
So this allows you to determine how far the enemies should be actively patrolling or doing whatever they do, how close they need to be to attack (can be altered if they are damaged and on high alert or whatever) and also finally line of sight.
I think I might have to try out changing the distance checks and resulting line of sight checks to have some slight delays, every update is good for gameplay but I think you are right it should be at least slightly delayed for performance.
Method 1 is the best(easiest), but to make it even out, you should add a Random.Range factor, so that they don’t check at the same time, but at individual cycles. Method 3 could be used if the number of zombies becomes very large.
I used this system in my own AI (Shooter AI), which is now on the Asset Store. It also contains a ready advanced zombie prefab.
This sounds like a smart solution to making an optimized zombie AI, do you have a link to a source, where someone explains the method?
– frankyboy450I have it in a book, but that's at home. I'll have to get the name of the book. Let me see if I can find something related online.
– ThePunisherAlright, Thanks :)
– frankyboy450Grr, I'm unable to find the book so I'm not entirely sure what to google in terms of the technique. I did, however, find a closely related technique that is used for collision detection and raycasting. It is called Bounding Volume Hierarchy (BVH). Just google that and check it out. There are definitely examples and tutorials available for it. If you look hard enough you should find a similar type of implementation to what I was mentioning earlier.
– ThePunisherDon't worry about that, thanks for looking for it ;) I found a blog about BHV and I'll look into it. It looks like a smart thing to learn.
– frankyboy450