This article contains a link to a Unity3D script that you can attach to a voxel object imported by PicaVoxel to generate BoxColliders for it at runtime.
Recently I downloaded MagicaVoxel which lets you create and modify blocky objects, and bought PicaVoxel which lets you import them into Unity3D projects. The purpose was twofold: to help my friend Highsight develop his first game, and to have access to powerful tools in case I ever wanted to make a voxel art game.
Highsight claimed difficulties with Rigidbody objects flying through the imported voxel objects. Upon looking at how the MeshColliders were built for the voxel objects, I guessed that the issue was with the fact the colliders were paper thin. Seeing that all of the voxel faces were one-sided, I figured that the best way to handle the issue was to create a BoxCollider behind every face.
The script below, when attached to a voxel object, will iterate through all Chunk objects and generate bounding boxes for them on simulation startup:
The script has two properties
- thickness: How thick the colliders should be (0.4 works well)
- staticColliders: If true, all the colliders will be static
Keep in mind that if your voxel object is static, the script will have problems accessing the mesh data. You can address this by making the object not static.
Because of the time it takes to generate the BoxColliders, you may want to consider writing an Editor script based on PicaBoxColliderGenerator to generate those boxes at design time instead of run time.
Here is how the scene looked with no colliders, and how it looked with box colliders.
On testing it with box colliders, I had no issues with Rigidbodies flying through the voxel at reasonable speeds. Highsight found the same to be true as well.
I hope you find this helpful!