Hiero Match : Polish Your Game a Little bit ‘Shine’

Unity 3 Blueprints

Unity 3 Blueprints

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 start from chapter 2 and finished to chapter 3.

In this post, I will show you how to polish your game to make it more shine.  we will add a game music that’s play on every scene and we also will add some sound effect to your game. Music has been essential part of the game since decades ago. Some game’s music become the icon of the game itselft. Sound effect also takes important role here, it’s help player ‘feel’ the situation he face in the game, sound effect also contribute to create a mood of player.

First of all, We need music and sound effect to play in this game. you can download some music and sound effect assets via tweet and get it button below


Tweet&getit is powered by Viuu


1. Playing music on entire game

it’s quite easy to add your music to game, just create empty Game Object and rename it to music, drag your music from Project View ‘sfx folder’ (_assets/sfx/matchMusic) to our new Music game Object. But there is a little problem here. Since last Hiero Match tutorial, we have 2 scene, ‘menu’ and ‘game’ scene. Whenever we load new scene the game object that contains our music gone (move to garbage collection) and so the music stopped playing. We could create ‘Music’ to each scenes, but this don’t solve all problem we have. the music will start from the beginning, it ain’t smooth transition isn’t it?

. rest assured, some nice guy on Unity3D forum share very cool simple class to use to keep gameObject alive when we load new scene. Using very nice singleton pattern, What this basically does is: Check for an existing instance, if there already is an instances, destroy self – if not, store this is instance (so anyone who’s coming later will destroy themselves). This also provides a public accessor so you can access the single instance from anywhere via MyUnitySingleton.Instance. Of course, you’d have your sound-specific methods also in that same class/script. Here is our singleton Class written in C-sharp on myUnitySingleton.cs script.

using UnityEngine;
using System.Collections;

public class myUnitySingleton : MonoBehaviour {

	private static myUnitySingleton instance = null;
	public static myUnitySingleton Instance {
		get { return instance; }
	}
	// Use this for initialization
	void Start () {

	}
	// Update is called once per frame
	void Update () {

	}

	void Awake() {
		if (instance != null && instance != this) {
			Destroy(this.gameObject);
			return;
		} else {
			instance = this;
		}
		DontDestroyOnLoad(this.gameObject);
	}
	// any other methods you need
}

Put this myUnitySingleton script into ‘you don’t want to destroy’ game object components on Hierarchy View (drag it and drop it into music Game Object). Don’t forget to put this ONLY on your first order scene which is scene ‘menu’, not in each of scene. don’t forget to check Play on awake and Loop option on your music game objects component. Also set the volume and pitch for your best interest.

Game Object 'Music' Value

Game Object ‘Music’ Value

2. Playing Sound Effect whenever tiles Slide, timer speeding up, finished game or failed game

we now move to scene ‘game’ and select tileGenerator Game Object, we have to add Audio Source Component to this gameobject so we could play our sound effect. On Inspector View, click add component, select Audio > Audio Source. Now we have required Audio source, next we open tileGenerator script and add these variable below to store our sound effect assets.

//define our soundClip
var soundTileSliding : AudioClip;
var soundBooing : AudioClip;
var soundCheers : AudioClip;
var soundTicking : AudioClip;
// we need to define isPlayed variable to state if Booing or Cheering is already played
// because we will call this sfx on Unity3D Update() function, which is looped through game time
var isPlayed = false;

Now we have our container ‘AudioClip’ variable, we should put our sfx assets into these variable via Inspector View, see image below for order of sound effect we should put

sound effect

sound effect

Now we have our sound effect on our tileGenerator script. we just need to call audio.PlayOneShot(SoundClip, float volume) function whenever we want to call our sound effect. for our case we put it like this :

a. Booing and Cheers Sound, we need to play it whenever we finished the game, cheering will be played if we finish all of tile, and booing will be played if we run-out of time. We put it into Update() function, and checking if the sound already played and run out timer or run out tiles. here is the example

if (this.isPlayed == false){
	if (finished == true){
		//play cheering sound
		audio.PlayOneShot(this.soundCheers,0.5f);
		isPlayed = true;
	}

	if (timeUp == true) {
		//play booing sound
		audio.PlayOneShot(this.soundBooing,0.8f);
		isPlayed = true;
	}
}

b. Ticking sound, we need to play it whenever our timer speeding up (see tutorial 3). Put this line below before we decrease our timer on revealCardTwo() function.

//play ticking sound
audio.PlayOneShot(this.soundTicking,1.0f);

 c. Sliding Tile Sound, we need to play it whenever our tiles sliding / move due too new challenge create on tutorial 5. Put this line below before we call slideTiles() function on revealCardTwo() function.

audio.PlayOneShot(this.soundTileSliding,1.0f);

3. Rig Scoring mechanism (bigger score added for early time)

Player need to compete with others player, so we need mechanism to get highest score which each people will get different depend on how fast he finished the game. So we need to rig scoring mechanism to follow new rule ‘Bigger score for faster time’. The logic is simple we just need to add timer (roundedSeconds) variable into our scoring mechanism. here is the example

//score added
scoreInt = scoreInt + (1+(roundedSeconds/10));

feel free to create your own scoring rules, it’s your own game that you create now.

Now you can test it with play button on unity3D windows, and Voila, you have better game now !

You can try the modified game here >>

and also checking our Hiero Match tutorial.
1. 2. Hiero Match : Adding Exploding Particles Whenever Tiles Removed
3. Hiero Match : Speeding Up The Timer Whenever Tiles didn’t Matched
4. Hiero Match : Adding Start Menu GUI
5. Hiero Match : Add New Challenge “Rotating Tiles”
6. Hiero Match : Polish Your Game a Little bit ‘Shine’

One Opinion to this post

  1. [...] Match : Adding Start Menu GUI 5. Heiro Match : Add New Challenge “Rotating Tiles” 6. Heiro Match : Polish Your Game a Little bit ‘Shine’ : we will add a game music that’s play on every scene and we also will add some sound [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment moderation is enabled. Your comment may take some time to appear.