In Part I we created three materials which we will apply on the sides of every cube. In this part we will assemble it from separate quad models which we will position appropriately. Unity offers simple built-in models (cubes, capsules, planes etc.) that can be used in your project. In order to create a voxel we would be tempted to use a Unity built-in cube. The main problem about this is that you can’t map different textures on each face. Thus, we are forced to create one from scratch, using quads.
This is a series of tutorials oriented towards the research and building of a minimal Minecraft-like game while exploring different facets of the Unity game engine. As this is an introductory tutorial, and because of the need to simplify (for didactic purposes), the algorithms presented in Series 1 are not in any way optimal, nor the structures of the objects used in the game are efficient. Presenting a trivial, fast way to build a minimal Minecraft-like game offers an introductory experience to people just stating to learn Unity, and in the same time, it arises the imperative need for optimization and efficiency. Over the course of the following tutorial series we will cover different implementations with higher degree of complexity.
I recommend to you, The Reader, to state any issues in the comments below. I encourage you to debate anything and everything regarding the contents of this tutorial, for the benefit of the community. Your contribution will help make these tutorials better!
We would be tempted to use Unity’s primitive built-in cube as a basis for our Minecraft cubes and map each corresponding texture from a texture atlas on each cube face using UV coordinates, but in this tutorial we are going to keep it simple (with as few external assets as possible) and use separate meshed for each side.
On the upper left menu, click on GameObject -> Create Other -> Quad. Repeat this action five more times (in total we need to create the six sides of a cube).
Now rename each of the six quads with these names:
Top, Bottom, Right, Left, Front, Back.
The objects present in a scene are called GameObjects. To rename a GameObject, right click on it in the Hierarchy Tab and click Rename.
When created, the GameObjects are placed apparently random in the Scene (in fact new GameObjects are placed in the Scene Camera target position). We need to position each face of the cube. To achieve this, in the Hierarchy tab click on each quad and modify in the Inspector tab their position and rotation like this:
Voila! We now have a grey cube:
If the cube is not centered in the Scene viewport, double click on one of the quads from the Hierarchy tab to reposition the camera.
In the Project tab, enter the Materials folder. In order to create a nice pixelated cube we need to drag and drop:
- TopMaterial over the Top quad
- BottomMaterial over the Bottom quad and
- SideMaterial over the Right, Left, Front and Back quads, located in the Hierarchy tab.
Great! This looks like a Minecraft voxel but now we actually have six separate quads, and not a stand-alone GameObject to instantiate in our world. To transfer these quads into a single GameObject we will use Unity’s simple to use hierarchy system. This basically allows any GameObject to become the child of any other GameObject from the Scene by dragging and dropping it onto the desired parent. This is extremely useful because the child’s Transform (position, rotation and scale of an object) will remain relative to the parent’s transform.
On the top left menu, click Game Object -> Create Empty. This will create an empty GameObject which will only contain a Transform component.
Right click on the newly created GameObject and rename it.
Left click on it and set its position in 0,0,0.
Now, select the six quads and drag and drop them on the empty GameObject.
If you happen to encounter errors like in the image below, keep in mind that this is a common occurrence when manually changing a GameObject parent-child hierarchy. Just press Clear on Play in the Console tab to clear the last error log when running the game.
In Part III we will add clicking functionality to our cube.