From zero to a complete Unity3D game: 3D Concentration using C# – step 1

Time to make things serious with Unity3D with the launch of a tutorial series which will guide you from zero – and I really mean ZERO: no knowledge – to the creation of a complete Unity3D game using C# programming language.

We’ll create Concentration game, it’s one of the six games you must be able to make in less than a day so here we go:

First, create a new project, give it a name and forget about including additional libraries. We don’t require them.

Once we created the new project, we need a Game Object which in this case will be the game itself. Call it MainGame

And finally, let’s script something: create a new C# script and call it MainScript, then double click to open it in Unity3D default editor

Most of the script you need at the moment is already written so you’ll need to add only one line:

Let’s have a look at the script: we have default inclusions (lines 1 and 2), class declaration (line 4, sounds like public class MainScript extends MonoBehaviour if you are familiar with AS3), a Start function which is the first function called by the class so it’s basically the constructor and Update function which will be executed at every frame.

At the moment we just want to write something on the console log, so we can say Debug.Log is our old AS3 trace.

Save the script and drag it to MainGame Game Object, it’s the way you bind a script to an object.

Run the project and you will see your “Hello World” in the console log.

Congratulations! Your made your first step and created a working project. Now it’s time to create the main actor of a Concentration game: the tile. We can imagine the tile like a cube primitive with a very short height, some kind of a 3D tile. So here we go with the creation of a cube:

Line 8: creation of a primitive Cube called cube of GameObject type. This will create the default cube which vertices have a length of one unit. Don’t worry about units now, let’s say 1 meter, or 1 centimeter, never mind.

Line 9: Time to give the cube the dimensions we need, so we are setting x and y to 2 and z to 0.1f. “f” is not a typo, it’s the way we tell C# we are dealing with a float variable.

Line 10: This is how we paing the cube with a full red.

Run the script, and here it is the tile cube. At a first sight, it may look like a box shape, but believe me it’s a cube, it’s just the camera view. Also, look how the cube is listed in the object hierarchy.

Obviously the cube will have its own class, so we are binding a script to the cube at line 11 of the next script which says the cube should be bound to CubeScript script

Then it’s easy to create another C# script, call it CubeScript and place another hello world debug text:

Run the project and you will see your Hello World Cube Edition message in the console.

But we didn’t create this script to write something in the console, the truth is we need it to make the cube react to mouse clicks, so here we go with some kind of mouse listener:

Lines 17-20 wait for a mouse click then paint the cube with a full green. Run the project, click the cube and watch it become green.

Now, in a true 3D Concentration game, you should be able to flip tiles, and that’s what we are going to do now:

This is a bit more complicated, so we’ll comment it line by line:

Line 6: a global Boolean variable called flip, will tell us if the tile has to flip

Line 16: in the function executed at every frame, we check if we have to flip the tile

Line 17: let’s meet the Quaternion type, used to represent rotations. At the moment, let’s just say we store in currentStep variable the current tile rotation.

Line 18: this is the final cube revolution represented by a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis. In our case, 180 degrees around x axis, so we flip it horizontally.

Line 19: finally we interpolates between current step and next step in a given time. This will make the flip animation accelerate in the beginning and slow down in the end with some kind of non-linear easing.

Line 25: set flip to true because the cube has been clicked.

Run the project again and see how you can flip the cube with a mouse click. Unfortunately, there’s nothing to stop the rotation and although you won’t see it, the cube continue to try to rotate according to current and final rotation. To prevent this just add these lines:

Basically we stop rotating when y rotation is -1 (upside down) thanks to rotated Boolean variable.

Run the project, click the cube and at the end of the rotation, enjoy the debug console message:

That’s all at the moment, you managed how to create and interact with cubes.

Download the project. Next time, adding texture to the tile.