How to handle large amounts of actors and AI?

Currently making a twin stick shooter and I’m noticing I’m getting a lot of lag when the player starts firing a lot of projectiles rapidly or when a lot of enemies are spawned in (roughly 300 simple ai, they only move directly to the player on a small plane). I’m just wondering what I can do to get around this issue. Thanks.

Hi! I think in case of large number of projectiles you can use “Object Pooling”. The idea is to reuse objects but not destroy them. I found the plugin on the marketplace wich, i think, can help you with that (i am not test it. It just an option):

In case of AI, i think, sometimes you don’t need AI at all. It can be simple actors without any “brains” and sophisticated logic. Like projectiles.)))

Thank you, the object pooling solution seems to be the sort of thing I’m looking for.

As for the AI, I’ve tried to do them in various ways using characters pawns or actors and all of them seem to cause the same performance hit just from calling simple move to actor on a regular basis. Do you have any idea how I could get around this as I’m not sure how much more simple I could make the enemies haha

I think, before any optimizations, you need to find the exact cause of the lags. Is it rendering or AI pathfinding/logic. I found this thread, wich can be helpful:

To find the exact reason i recommend you to start from simplest solution. Like actor, without navigation, Just “dumb” setactortransform in direction towards player every tick.
Try use performance profiling tools to identificate the problem.

So I’ve looked into this a bit more and done some profiling and it seems like the main cause is due to the collisions between enemies.

I’ve got 2 sphere collisions on each enemy, one is the hitbox to damage the player and the other is smaller and allows the enemies to partially overlap but not all the way. If I disable these and just have the enemies move towards the player I go all the way down to 0.7ms frame time but as soon as I enable even 1 of them that jumps up to 10ms or more.

According to the profiler the main cause of the performance hits are mostly on “GeomSweepMultiple” “DispatchBlockingHit” and “MovementComponent ResolvePenetration”. Do you have any idea on how I could improve the performance of these collisions?

Any chance you are working on zombie apocalypse game?)))
Do you use navigation mesh?
To minimize overhead on navigation (i think this is one of the problems) you can try to “merge” AI actors to clusters. Something like boid flock movement. If AI actors close enough to each other - they begin to share navigation. Because 300 actors at the same screen, i think, will look so messy, so this hack will not be noticable. I think, you can even disable or merge collision in clusters.
You can also use animation sharing:

I hope this helps you.)

4 Likes