Build Minecraft in Unity Part 2: Voxel Creation (a fast and inefficient approach)

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).

19_quads

Create six quads

Now rename each of the six quads with these names:

                                            TopBottomRightLeftFrontBack.

The objects present in a scene are called GameObjects. To rename a GameObject, right click on it in the Hierarchy Tab and click Rename.

20_rename

Rename each quad

Before we continue, if you are just beginning to learn about Unity, it is HIGHLY recommended that you read about Scene View Navigation and Positioning GameObjects!

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:

22_pos_rot_faces

Transform values for each quad

Voila! We now have a grey cube:

21_faces

An average looking, 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 RightLeftFront and Back quads, located in the Hierarchy tab.

Applying materials on Scene GameObjects

The result:

24_voxel

A textured cube

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.

25_createempty

Create an empty GameObject

Right click on the newly created GameObject and rename it.

26_voxel

Rename the empty GameObject

Left click on it and set its position in 0,0,0.

27_position000

The GameObject is positioned in the center of the Scene

Now, select the six quads and drag and drop them on the empty GameObject.

Quads Assemble!

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.

part2_feedback1

Common manual  GameObject hierarchy change error

Don’t forget to save the scene! Now, if you click on Voxel you can translate it by clicking 29_translate, rotate it by clicking 30_rotate or scale at will it by clicking 31_scale, while in the Scene tab.

32_transforms

Playing around with Positioning Gizmos

 In Part III we will add clicking functionality to our cube.


blog comments powered by Disqus