- Home /
15625 blocks are making my game lag? Increase performance?
I am trying to make a terrain with 15625 blocks, and I plan to make a bigger world, so I will have much more blocks in the future. I am having a hard time trying to optimize my game so it doesn't lag, but I cant seem to find a solution.
Heres a picture with the profiler and scene opened: https://prnt.sc/o02br5
For what reason are you using blocks? is it to destroy them and build stuff like $$anonymous$$inecraft?
The problem is (most likely) that all of your blocks are rendered even though they are not visible. If you procedurally generate them, then modify your script to disable blocks which have a block on top of it, and when you break the block then check if there's a block under it or not, and then enable it back.
Other than that, of course your game lags because in the profiler you render all 15625 blocks at the same time because your camera can see all of them. If there were a perspective for e.g.: first-person perspective, then the other blocks would be disabled because of occlusion culling.
I am making a RTS type game where you can build and destroy blocks, and also their are no blocks underneath the top blocks. All 15625 blocks are the ones on top. I made a video showing more of the game, but the performance is better here because I used less blocks than in my current situation. https://www.youtube.com/watch?v=_chifrceGQA
Answer by Razor1994 · Jun 10, 2019 at 05:41 PM
Okay where should i start? :D You should NOT generate your terrain using just blocks. What you need to do is generate the world out of Quads. You should only generate the quads that you are able to see. Also you should combine the meshes of your quads to single meshes (chunks) (lets say 16 blocks combined to one big mesh is one chunk). That way your terrain will be generated much much faster. I am using a voxel terrain for my current project too, so maybe i can add some screenshots later :D
-Best regards RaZor
wouldnt each face of the blocks count as a quad? how would I go about hiding the quads I dont see?
A cube is basically constructed of 6 quads. But there is a critical difference.
Here is what my terrain looks like from the bottom ![alt text][1] [1]: https://prnt.sc/o03wdw
if you look at your terrain from the bottom you can probably see the bottom of your cubes. Thats the problem. If you are using cubes you are rendering quads that the user will never see. These are not necessary and do affect performance quite a bit. So thats why you need to only render the quads that the player is able to see.
I know why I need to render only the quads I see, but I cant find out how im supposed to hide these quads, how would I go about doing this myself?
Answer by DONlX · Jun 10, 2019 at 07:23 PM
The fastest solution is set a draw distance of your player, with SetActive(bool), it can fix the problem. The lag is caused by the 15625 box colider, physical calculations have a havy CPU usage. You can try the following code:
GameObject player;
List<GameObject> objects;
float distance = 1000F;
foreach (GameObject g in objects)
g.SetActive(Vector3.Distance(g.transform.position,player.transform.position) < distance);
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
World Generation with Interesting Terrain 0 Answers
Voxel Brush throwing Null Reference 1 Answer
[C#] 2D infinite Terrain Generation with prefabs?! -> lag 0 Answers
Add perlin noise to blocky terrain 0 Answers