From zero to a complete Unity3D game: 3D Concentration using C# – step 3: multiple instances of the same object and camera movement

Read all posts about "" game

In this third step of the 3D concentration game we’ll see how to create multiple instances of an object, how to move the camera and how to add a background plane.

If you have missed them, check from zero to a complete Unity3D game: 3D Concentration using C# – step 1 for the bare bones and step 2: skinning and timing to know how to join game objects and handling timers.

Basically, at this point we have a tile which can be flipped but now we want an arbitrary number of tiles placed in rows and columns, moreover we want to place the camera in order to see all tiles no matter of their number, and a plane where to lie tiles.

It may seem a lot of things but actually it’s very easy, although I had to modify Mainscript.cs a bit:

using UnityEngine;
using System.Collections;

public class MainScript : MonoBehaviour {
	
	void Start () {
		int rows=4;	// number of rows 
	 	int cols=5; // number of columns
	 	int tileSpacing=1; // space between two tiles
	 	int tileSize=2; // tile size
		for(int i=0; i<rows; i++){
			for(int j=0; j<cols; j++){
				// 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.transform.localPosition = new Vector3(3*j,3*i,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(3*j,3*i,0.251f);
				// attaching the quad to the cube
				quad.transform.parent=cube.transform; 
			}
		}
		// the camera, we want to point on the middle of the board
		float cameraX = ((cols-1)*tileSize+(cols-1)*tileSpacing)/2f;
		float cameraY = ((rows-1)*tileSize+(rows-1)*tileSpacing)/2f;
		// setting camera height, this should work with every number of tiles
		float cameraZ = Mathf.Max(cols,rows)*-3f;
		Camera.main.transform.localPosition = new Vector3(cameraX,cameraY,cameraZ);
		// the ground
		GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); 
		plane.transform.Rotate(-90,0,0);
		plane.transform.localPosition = new Vector3(cameraX,cameraY,1);
		plane.renderer.material.color = new Color(128,128,128);
		plane.transform.localScale = new Vector3 (cameraX/2.5f,0,cameraY/2.5f);
	}
}

While I only added one line to 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
					renderer.material.color = new Color(255,0,0); 		// set the color to red again
				}
			}
		}
	}
	
	void OnMouseDown() {
		if(!flip){
			renderer.material.color = new Color(0,255,0);				// set the color to green
			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 tiles to flip them.

Now, we are ready to use real graphics, which will be explained next week.

Download the source code of the entire project.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers