I'm making a top down shooter and I face a problem which is significant slow down (320fps down to 140fps) when firing an infinite clip automatic weapon.
the bullet at the moment is a cylinder of 18 polys (easily lowered) and I noticed not a single one of them is batched, if that is the issue, how could I get this to work?
Another thing that may be the issue is on firing it creates a new bullet prefab which on creation gets its rotation, does a raycast as far as its range, sets the bullet length (stopping it when it hits a wall and stopping it at its range if it does not) the materials alpha is then decreased for a second until the bullets alpha is less than or equal to zero at which point it destroys its containing object.
Is this poor practice? if so how can I improve its efficiency? I'd like to make it as fast as possible so it can run on lower end machines.
Any and all help is greatly appreciated,
Thanks in advanced, Bombshell
You must use object pooling for the projectiles.
In a nutshell it instantiate all the bullets when the scene loads, and then it reuses those GameObjets over and over again without the need to instantiate new ones.
Here is the code I wrote to tackle such issue:
ObjectPooling.cs (Attach it to an empty GameObject):
SpawnPool.cs (Attach it to a GameObject that will spawn the bullets, in your case the ship or soldier holding the gun):
AutoDevolvePool.cs (Attach it to a bullet Prefab):
Hope that helps!