Well optimized pather script for many AIs at once?

Does anyone have a well optimized pather script similar to AIPath.cs for large groups of AIs that are constantly moving?

I’ve made a few changes to AIPath.cs for my game but I’m looking for a similar script that would be able to more easily handle 200+ AIs on the screen at once. I’m making a tower defense style game with many many AIs at once but I’ve noticed the AIPath.cs script is eating quite a bit of resources since the AI’s target is constantly changing. It’s not a normal TD game where enemies choose one path and stick to it. In my game, the enemies are constantly moving counter-clockwise around the player’s base and thus their target is always changing.

Anyone have some simple scripts that I can try out for optimization purposes?

Hi

Try the AILerp script. That uses much simpler movement so the code is a lot faster.

Hey Aron,
I noticed you reccomended AILerp script for this guy. I have ~300 AIs on the screen at once and I want to push to about 500 but i am using AIPath which you do not reccomend. I looked at AILerp and it doesnt contain next waypoint distance or stopping distance which I need. Is there another lighterweight alternative that I can use or a way to get AILerp to have a nextwaypoint distance and stopping distance? I am using RVO’s. Thanks,

Hi

If you are already using AIPath in your game it might be tedious to switch it out. I would then recommend that you try to remove the features from the AIPath script that you are not using. Also try to reduce the frequency of path recalculations.

I have the repeat rate set at 5, I think that is pretty high, right? It seems most of the overhead is coming from AIPath.update() which I think has a lot of calculations and is always running. Does this only run when a new path is requested? I am not at my gamedev computer at the moment so i cant check. Is there a lighter weight AIPath that you know of on this forum I could test? One more thing (and thanks again btw :slight_smile: ) have you ever tried to animate with a lot of units on the screen? Its beginning to consume some pretty significant overhead and was wondering if you knew of any optimization there. Thanks Aron!

Hi

The Update method is a Unity built in method and it is called every frame.
There is no more light weight version of AIPath that I have.

In a game I have been developing which used a lot of skinned meshes, actually a very large overhead was just the time it took to set transform.position and transform.rotation. If the object has lots of children (as is often the case with skinned meshes because they have all their bones as individual transforms), then when setting transform.position and transform.rotation it will have to go through all those bones and update them. Also use culling and disable characters that are far away.

Hey Aron,
Thanks for the reply. Im using 2D units, on a 3D plane with RVO. Im using culling which helped me by almost 5fps which was pretty nice. Im going to lessen the amount of path calls and repeated path calls to see if that helps. Something else I noticed from the profiler was a large amount of Raycast calls coming from RVO collider. Is this normal with around 250-300 AIs at once? If not, do you know a way to reduce it? Thanks,

Hi

If you are using 2D units you should be able to remove the raycast calls altogether. They are only useful if you actually need to position units on the ground. You could also download the latest RVO beta version which has support for RVO in XY space. However that version has some quite significant changes to the RVO system, so things may not work out of the box after upgrading.