Unity 2D Whack-a-Mole Tutorial
Foreword
Let's use the Unity engine to make a 2D Whack-a-Mole game and build it for Android.
Whack-a-Mole (originally also Whac-A-Mole) is a funny redemption style arcade game that was invented around 1976. The goal of the game is to whack the moles that are coming out of their holes every few seconds. The faster the player hits the moles, the higher the score will be.
Our game will be rather easy, as it only requires 25 lines of code. As usual, everything will be explained as easy as possible so everyone can understand it. We will cover every single step that is required to make the game and then go through the process of building an App that works on Android.
Requirements
Knowledge
Our Tutorial does not require any special skills. If you know your way around Unity and heard about GameObjects, Prefabs and Transforms before, then you are ready to go. And if you didn't, don't worry about it too much.
Feel free to read our easier Unity Tutorials like Unity 2D Pong Game to get used to the engine.
Unity Version
Our Whack-A-Mole Tutorial will use Unity 5.0.2f1. Newer versions should work fine as well, older versions may or may not work. The free version of Unity 5 now comes with all the engine features, which makes it the recommended version.
Project Setup
Time to start developing. We will open Unity and select New Project:
We will name it whack-a-mole, select any location like C:\, select 2D and click Create Project:
Afterwards we save it once via File->Save Scene with the name "scene" (without the "").
If we select the Main Camera in the Hierarchy then we can set the Background Color to a nice green tone (like r=25, g=75, b=55) and adjust the Size like shown in the following image:
The Hole
We will begin by creating a hole with our drawing tool of choice:
Note: right click on the image, select Save As..., navigate to the project's Assets folder and save it in a new Sprites folder.
Let's select the image in our Project Area:
And then change the Import Settings in the Inspector:
Note: a Pixels Per Unit value of 32 means that 32 x 32 pixels will fit into one unit in the game world. We will use this value for all our textures, because the mole will be 32 x 32 px later on.
Now we can drag the image from our Project Area into the Scene to add it to the game world:
Let's select the hole in the Hierarchy and then position it at (0, 0) in the Inspector:
If we press Play then we can already see it in the game:
The Hole Script
The hole's job is to take care of the mole. It should spawn one, then destroy it after a few seconds and then spawn another one after a while.
This kind of behaviour is always implemented with Scripting. We can add a new Script to the hole by first selecting the hole in the Hierarchy and then pressing Add Component->New Script in the Inspector:
We will name it Hole and select CSharp as the language. Afterwards Unity creates a new Script in our Project Area. Let's keep everything nice and clean and move the Script into a new Scripts folder:
We can double click it to open it:
using UnityEngine;
using System.Collections;
public class Hole : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
We won't need the Update method, so let's remove it:
using UnityEngine;
using System.Collections;
public class Hole : MonoBehaviour {
// Use this for initialization
void Start () {
}
}
We will need a Spawn function that should spawn a new mole every few seconds. We can use Unity's Invoke function and some randomness to make this possible:
using UnityEngine;
using System.Collections;
public class Hole : MonoBehaviour {
// Spawn Interval
public int intervalMin = 2;
public int intervalMax = 10;
// Use this for initialization
void Start () {
Invoke("Spawn", Random.Range(intervalMin, intervalMax));
}
void Spawn() {
// ToDo: spawn the mole
// Next Spawn
Invoke("Spawn", Random.Range(intervalMin, intervalMax));
}
}
Note: we simply use Invoke in the Start function to call the Spawn function in a few seconds (something between intervalMin and intervalMax). Afterwards the Spawn function will use Invoke to call itself again after a few seconds (and so on).
Now we can add a public GameObject variable that allows us to specify which mole to spawn. We will then use Unity's Instantiate function to load the mole into the game world:
using UnityEngine;
using System.Collections;
public class Hole : MonoBehaviour {
// Mole Prefab
public GameObject mole;
// Spawn Interval
public int intervalMin = 2;
public int intervalMax = 10;
// Use this for initialization
void Start () {
Invoke("Spawn", Random.Range(intervalMin, intervalMax));
}
void Spawn() {
// Spawn the mole
GameObject g = (GameObject)Instantiate(mole, transform.position, Quaternion.identity);
// Next Spawn
Invoke("Spawn", Random.Range(intervalMin, intervalMax));
}
}
Note: transform.position tells Unity to spawn the mole at the hole's position. Quaternion.identity makes sure that it has the default rotation. . . .
Premium Tutorial
Enjoyed this preview? Become a Premium member and access the full Unity 2D Whack-a-Mole Tutorial!All Tutorials. All Source Codes & Project Files. One time Payment.
Get Premium today!