Hiero Match: Add New Challenge "Rotating Tiles
Part 5 of the Hiero Match series: rotate remaining tiles around the board after each match, using animation and temporary parent objects.
Hiero Match is a matching/pairing game tutorial using Unity3D from the book Unity 3 Blueprints, written by Craig Stevenson and Simon Quig, and published by Deep Pixel. This tutorial spans from chapter 2 through chapter 3.
This post adds a new challenge: whenever tiles are matched, the remaining tiles' positions move clockwise or counterclockwise based on their current locations.
Challenge Concept
This feature requires players to maintain greater focus to remember tile locations and track how far tiles have moved. To animate the tile movement, new animations are needed:
- Slide up (position y + 1.5)
- Slide down (position y − 1.5)
- Slide left (position x − 1.5)
- Slide right (position x + 1.5)
These animations must be appended to every tile prefab. (In-depth animation curve instruction is covered in chapter 3 of the book.)
Step 1: Add Variables
//define variable to be pointer to selected game object
var selectTile : GameObject;
//define variable store poition x coordinate of selected game object
var selectPosX : float;
//define variable store poition y coordinate of selected game object
var selectPosY : float;
Step 2: Create slideTiles() Function
function slideTiles(){
//looping for all tiles game object from tileObjects
for (var i = 0; i < tileObjects.Length; i++){
//find gameobject still exist on the scene
if (GameObject.Find(tileObjects[i].name+"(Clone)")){
//gameobject assigned to selectTile variable as temporary pointer
selectTile = GameObject.Find(tileObjects[i].name+"(Clone)");
//create temporary 'Empty' game object to be parents of selected tiles
//because animation position will be started from global / parent world (0.0.0)
//so we will create Empty Game Object to contains selected tiles, so it's animation
//will start from it's parent position
var temp : GameObject = new GameObject("Temporary");
temp.transform.position = selectTile.transform.position;
temp.tag = "Temporary";
selectTile.transform.parent = temp.transform;
print("select find tiles : " + selectTile.name);
//get selected tiles X-pos and Y-pos
selectPosX = selectTile.transform.position.x;
selectPosY = selectTile.transform.position.y;
//get tiles that move left
if (((selectPosY == 4.5) && (selectPosX != 0)) || ((selectPosX == 3) && (selectPosY == 1.5))){
selectTile.transform.animation.Play("slideleft");
//get tiles that move down
}else if (((selectPosX == 0) && (selectPosY != 0)) || ((selectPosX == 3) && (selectPosY == 3))){
selectTile.transform.animation.Play("slidedown");
//get tiles that move right
}else if (((selectPosY == 0)&&(selectPosX != 4.5)) || ((selectPosX == 1.5)&&(selectPosY == 3))){
selectTile.transform.animation.Play("slideright");
//others tiles move up
}else{
selectTile.transform.animation.Play("slideup");
}
}
}
}
The function uses parent-child relationships in Unity3D to ensure animations start from the tile's current position rather than the global origin. An empty GameObject is created as a parent, positioned at the tile's location, allowing proper animation initialization.
Step 3: Clean Up Temporary Objects
function deleteEmptyTemporary(){
//find all Temporary game object
var arrayObj : GameObject[] = GameObject.FindGameObjectsWithTag("Temporary");
for(var obj:GameObject in arrayObj){
//checking how much game object has child
var childs = obj.transform.childCount;
//if gameobject didn't have child, destroy it
if (childs < 1) {
Destroy(obj);
}
}
}
Since temporary empty objects are created during each iteration, they accumulate and consume memory; this function removes the unused ones.
Step 4: Integrate into revealCardTwo()
function revealCardTwo(){
//some code from revealCardTwo()
if (tName1[0] == tName2[0]){
//some code from revealCardTwo()
Destroy(matchOne);
Destroy(matchTwo);
//we start sliding tiles after we destroy two mathed tiles
slideTiles();
yield new WaitForSeconds(1);
//delete all empty temporary game object
deleteEmptyTemporary();
//some code from revealCardTwo()
}else{
//some code from revealCardTwo()
}
//some code from revealCardTwo()
}
When a match occurs, tiles are destroyed, then the sliding animation is triggered. After a brief delay, temporary objects are cleaned up.
This is part 5 of the Hiero Match series.