- Home /
Generating too many blocks crashes the game (Both exe and in editor)
I have created this minecraft-ish voxel game where the player can create a voxel map. I have a block prefab (nothing much of the ordinary) - it has a box collider, a texture, and a mesh renderer.
On my code, it just as simple as this:
public void GenerateTerrain()
{
for(int x = 0; x < mapSizeX; x++)
{
for(int z = 0; z < mapSizeZ; z++)
{
GameObject groundBlock = Instantiate(groundBlocks[0], new Vector3(x, transform.position.y, z), Quaternion.identity);
groundBlock.transform.parent = transform;
}
}
}
I think this works fine if you put in 32x32. However if the player tries to generate a 128x128 (16k blocks) or greater and tries moving around the map, the game crashes and even shutdowns the computer (i know it's scary).
I tried using the built in occlusion culling, it kinda increased the performance "a bit", however it still crashes.
How can I fix this?
Its better to build a single mesh ins$$anonymous$$d of each block being a gameobject. This is an good place to start learning voxels: $$anonymous$$sTV
Answer by Cornelis-de-Jager · Feb 01, 2019 at 12:58 AM
Unfortunately Standard Unity is not optimized well for creating many blocks. Its one of the Unity's downfalls, however, all hope is not lost.
Unity has already created the Entity Component System, which is a different way of programming in C# that optimizes memory allocation and retrieval, thus giving HUGE boosts to performance. You no longer use object orientated programming, however, so the learning curve is steep, but performance is worth the while.
Here is a video of a stress test where 500K cubes were spawned. CLICK ME.
If you want to use the ECS system you will need to download the correct versions of unity and enable it. CLICK ME TO SEE HOW
I have watched the videos. It's kinda neat. However I don't know how to implement ECS into my code though. How did you manage to get it all working in generating those 500k cubes?
Answer by AnOrdinarySandwich · Feb 01, 2019 at 01:11 AM
If you're not interested in switch to ECS, perhaps something simple like a proximity zone around the player that causes any groundBlock out of range to disable its collider. Could easily be set up with a trigger collider on the player and then use OnTriggerEnter to enable the block colliders, and OnTriggerExit for disabling (as well as being the blocks default state) the block colliders. Just a thought! :)
Nice idea! :)
I tried this one:
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("PlayerParent"))
{
mesh.enabled = true;
collider.enabled = true;
Debug.Log("Collided");
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("PlayerParent"))
{
mesh.enabled = false;
collider.enabled = false;
}
}
However it seems it doesn't actually detected the colliders? I created a sphere collider in my playerobject, tagged it as "PlayerParent", and set the colliders as a "isTrigger". However the 'collided' in the console doesn't seem to appear.
Okay I got the colliders working, I forgot to add a rigidbody on my player parent... however, the ontrigger doesn't seem to work.
Here's my updated code:
private void Start()
{
mesh = GetComponent<$$anonymous$$eshRenderer>();
collider = GetComponent<BoxCollider>();
mesh.enabled = false;
}
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("PlayerParent"))
{
mesh.enabled = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("PlayerParent"))
{
mesh.enabled = false;
}
}
At the start of the scene, the blocks that were kinda far from the player, their mesh renderers got disabled. However when I approach them, it won't enable them (it should). Also when I leave the blocks of which the mesh renderer is enabled, it did not disappear also (it should since onTriggerExit will be triggered right?)
Hi, it seems like the OnTriggerEnter (I wouldn't recommend using OnTriggerStay, as it will be called all the time on all the blocks within range of the collider) and OnTriggerExit are on the blocks, not on the player. I assume this from the tag check, looking for the other object to be a PlayerParent.
If so, try putting them on the parent ins$$anonymous$$d.