Hi! I want to create a swarm simulation with thousands of equal entities (i.e. bees or ants).
Each entity is rendered using the same sprite, using the same behavioural script.
When I move the entities in the respective script with transform.position += direction * Time.deltaTime in Update() my frame rate goes down by about 50%.
Is there any way to increase performance on this? FYI: Current cap on my reasonable PC is about 15000 sprites.
Can you skip frames to remove the amount of iterations over the sprite movements so limit the amount of calculations that are occurring?
Use something like WaitUntil to skip every X frame before moving the sprites, you can still get natural movement without moving 0.0000000002f(or whatever) each frame.
I don’t think there’s any way to simplify that line if you want to model each entity as a separate gameobject. The only better solution I can think of is to perform the movement on the GPU side via a shader, which will probably yield better performance, but means you won’t be able to do any CPU-side calculations based on individual entity positions (does your swarm need to collide with things, for example?).
There are various approaches to this sort of thing, but I would be tempted in your case to forget having a separate game object for each bee/ant, and instead generate a single mesh each frame, which contains a quad for each bee/ant.
So you create a single component on a game object called something like ‘SwarmComponent’ and internally that stores a big array containing your 1000s of ‘SwarmEntity’ structures. These aren’t actual unity gameobjects or anything, just your own little c# class that contains the minimal info you need for each entity.
The update for your swarm component then goes:
Update each of your ‘SwarmEntity’ objects
Build 1 big list of quads - 1 quad for each entity
Write them all into 1 dynamic mesh (have a search for that online - lots of guides to it!)
It’s a bit of a roundabout process, but if you’re moving in the direction of swarms of 1000s of objects, it’s going to take a bit of work to make it all fit nicely into unity.