- Home /
The question is answered, right answer was accepted
How can I reduce CPU cost of blocks in my Minecraft clone?
Hi, so I'm making a game like Minecraft because blocks are easy to work with, but the physical part of the block seems to take up way too much CPU. What I mean by physical is empty gameObject containing six quads and a box collider, rather than the type I made called 'Block" which can't be seen. I'm going to make it so that only exposed blocks are active, but there could easily be tens of thousands of exposed blocks active at a time, and my game starts to lag with only 5,000 of them. I don't know how Minecraft allows so many blocks onscreen at once without lagging, but I'd like to accomplish the same.
EDIT: Okay so I've found an even bigger problem. When I run a check Update for every block to see if I can disable it for better performance, it actually seems to be using even more of the CPU than if I didn't run the check and just left it active, which is not looking good. What I'm doing is checking every side of a block for an empty space, and then set the block active once it finds an empty space, or set it inactive if it doesn't find one. How does every other block-based game accomplish updating all the blocks without lagging due to the potential of millions of blocks?
//This code will run for every block.
if (blocks [x, y, z] != null) {
if (x + 1 < width) {
if (blocks [x + 1, y, z] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (x - 1 >= 0) {
if (blocks [x - 1, y, z] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (y + 1 < height) {
if (blocks [x, y + 1, z] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (y - 1 >= 0) {
if (blocks [x, y - 1, z] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (z + 1 < length) {
if (blocks [x, y, z + 1] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (z - 1 >= 0) {
if (blocks [x, y, z - 1] == null) {
Enable (physicalBlocks [x, y, z]);
break;
}
}
if (physicalBlocks [x, y, z].activeSelf) {
Disable (physicalBlocks [x, y, z]);
}
}
Answer by Bunny83 · Aug 22, 2018 at 11:01 PM
Sorry but there are already way too many minecraft related questions here on UnityAnswers
https://answers.unity.com/questions/56121/minecraft-style-grid-and-block-placement.html
https://answers.unity.com/questions/623541/minecraft-style-block-placement.html
https://answers.unity.com/questions/506349/build-system-for-minecraft-lookalike-game.html
https://answers.unity.com/questions/700004/block-lightning-minecraft-like.html
https://answers.unity.com/questions/392185/how-do-i-make-cube-grid-like-minecraft.html
https://answers.unity.com/questions/1126340/which-voxel-terrain-solutions-exist-for-unity.html
https://answers.unity.com/questions/177931/tutorial-minecraft-chunks.html
https://answers.unity.com/questions/1477652/minecraft-style-culling.html
https://answers.unity.com/questions/254523/minecraft-rendering.html
There are also many forum posts on that topic:
https://forum.unity.com/threads/minecraft-style-game.177831/
https://forum.unity.com/threads/tutorial-procedural-meshes-and-voxel-terrain-c.198651/
https://forum.unity.com/threads/minepackage-minecraft-starter-package.69573/
And also other resources
People got already so bored with that subject they already done minecraft in one day challanges.
Minecraft is not an easy made game. Minecraft requires several advanced techniques you have to master. It looks like you don't really have done much research for your project.
ps: In Minecraft you don't see several 10k blocks at once but millions. Without chunking the world this is impossible to handle.
What is this "chunking of the world"? anyhoo, I just realized that I shouldn't be updating every block every frame, but am ins$$anonymous$$d going to only update them when something happens to them. Also, I found that I should be storing the blocks internally simply as data, and only create physical blocks when needed, and delete them otherwise. I know that there are millions of blocks at once in $$anonymous$$increaft, but I was just referring to their physical representations (graphics). Thanks for all the links, this question was mostly just about increasing performance.
Follow this Question
Related Questions
Minecraft block placing/ block building - how to? 2 Answers
Delete Blocks 1 Answer
(script) how to get terrain to turn into blocks 0 Answers
map generation issues 0 Answers