- Home /
Optimize performance of simple survival game
I am creating mobile survival top down 2d game. All renderers are sprite renderers, all colliders are box or capsule colliders. I wanted the world to be infinite, so I made world generating algorithm. Algorithm doesn't work slowly, but I notice that when number of generated gameobjects is more than 1000, the game starts lagging. The fps decreases from 120 to 0. I made algorithm that disables renderers and colliders (but not scripts) which are not in camera field of view. Didn't work. I disabled all gameobjects and it also didn't increase fps. The only thing that made fps higher is destroying all gameobjects. But I can't even disable them cause they have scripts that should always work. Scripts are simple, the maximum thing they do is counting time so they can't be the cause of lagging. How can I improve performance without destroying objects? Sorry, If I made mistakes in my English.
Did you test this in a build? If you are working from Editor on this issue you must realise that the Scene Camera also adds in to your total FPS and overall performance. To test this, in scene view, rotate the camera to the sky so it sees no objects and observe FPS.
Nothing changed. I think that the problem is the algorithm that disables rends and colls of far located gameobjects.
Ok, what does your profiler say? Its a shame, this wouldve been convenient :P
Answer by MadsWiering · Apr 01, 2018 at 10:49 AM
Maybe take a look at object pooling, which holds objects in memory with a fixed size, you reuse these when you travel along the world. Objects you passed get set to a location you still have to travel to use. This makes your memory consumption consistent.
And I hope you are using 2D Colliders instead of the 3D , since you said capsule collider.
Really (if you mean that the algorithm that disables far located objects is the main cause of low fps), I tried to disable that algorithm and fps got higher to 100 and started hesitating between 60 and 100 fps. Is enabling/disabling renderers and colliders of gameobject and changing its layer takes enough much time to make FPS lower? Note: I am using 3D colliders for several reasons.
You should not mix 2D and 3D. If you want to use 3D in your 2D game you should make it Psuedo 2.5D and not use the 2D features.
I use 3D colliders cause I can pull them among z axis so that z values of positions of objects can be different and 2D colliders will pass through each other. Their z values are different cause game is top down and z=-y to sort objects without using Sorting layers cause they're limited.