Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Concentration game, C#, Game development and Unity3D.

Time to show you the second step of our Unity3D Concentraton game. In step 1 I showed you how to create a cube primitive, resize and rotate it as well as react to mouse input and change color.

Now it’s time to add some kind of time listener to make it flip again once we saw its value, find another way to rotate it (actually there’s no need to find another way to do it but if you want to master a language you should know a lot of ways to do the same thing) and start texturing it.

Bad news first, you can’t apply different textures/colors to different faces on a primitive Unity3D cube. This means you can’t have a green cube with a black face. There are a lot of different ways to accomplish this task, from creating the cube itself with a 3D modeling software like Blender then playing with UV mapping to creating the cube programmatically, vertex by vertex, then assign different textures to different faces.

Since it’s just a concentration game and we don’t need such a great number of stuff on the screen, we can choose a dirty way to do the same using some unnecessary polygons, which won’t affect the overall speed anyway.

That’s why I added a primitive quad to the primitive cube, stick them together so I will be able to add a texture only on one (fake) face of the cube.

Look at MainScript.cs:

using UnityEngine;
using System.Collections;

public class MainScript : MonoBehaviour {

	void Start () {
		// a cube
		GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
        cube.transform.localScale=new Vector3 (2,2,0.5f);
		cube.renderer.material.color= new Color(255,0,0);
		cube.AddComponent("CubeScript"); 
		// a quad
		GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); 
		quad.transform.localScale=new Vector2 (2,2);
		quad.transform.Rotate(0,180,0);
		quad.transform.position=new Vector3(0,0,0.251f);
		// attaching the quad to the cube
		quad.transform.parent=cube.transform; 
	}
	
}

The other interesting thing about Unity3D is the way it handles timed function using Invoke. This way I was able to call a function which covers the tile 2 seconds after it has been shown.

This is CubeScript.cs:

using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour {
	
	private bool flip = false;			// I want to manually flip the tile
	private int speed = 5;				// rotation speed, must divide 180
	private int steps;					// the number of steps required to complete a rotation
	private int direction;				// rotation direction. 1 = show tile; -1 = hide tile

	void Update () {
		if(flip && steps<180/speed){	// we want to rotate it and the rotation is not completed
			transform.Rotate(Vector3.up*speed*direction, Space.World);	// rotate it around world coordinates according to speed
			steps++;					// we did one more step
			if(steps==180/speed){		// if the rotation is completed...
				if(direction==1){		// if we were showing the tile...
					Invoke("Cover",2);	// call "Cover" function (to cover the tile) after 2 seconds
				}
				else{
					flip=false;			// else set flip to false to enable the player click and show the tile again
				}
			}
		}
	}
	
	void OnMouseDown() {
		if(!flip){
			renderer.material.color = new Color(0,255,0);
			steps=0;					// resetting steps
			direction=1;				// remember, direction = 1 => show tile
			flip = true;				// we want to flip the tile
		}
    }
	
	void Cover(){
		steps=0;						// resetting steps
		direction=-1;					// remember, direction = -1 => hide tile
	}
	
}

And this is the result:

Click on the tile and watch it flip then after two seconds flip again.

Download the project. Next time, a lot of tiles on the screen!

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.