-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Unity Game Development Blueprints
By :

We have all the mechanics of our game completed at this point. Now, we need to actually create the game or manage what happens in the game. This game controller would be required to run our game, keep track of and display the game's score, and finally end the game whenever the player dies. Later on, we'll discuss a game state manager, which we can use for larger projects with multiple states, but for the sake of this simple project, we will create a single game controller. That's what we'll do now:
GameController
, and optionally, for neatness sake, set its Position to (0
, 0
, 0
).A Tag is a way to link to one or more game objects in a collected group. For instance, you might use Player and Enemy tags for players and enemies respectively; a Collectable tag could be defined for power-ups or coins that are placed in the scene; and so on. This could also have been used with EnemyBehaviour to check whether something was a bullet or not. One thing to note is the fact that GameObject can only have one tag assigned to it. Tags do nothing to the scene but are a way to identify game objects for scripting purposes.
GameController
. Then press Enter or click on Create and Add.Assets
folder of the Project tab, and move it to the Assets\Scripts
folder. Go to MonoDevelop by double-clicking on the script file.While our game does many things, the most important thing is the spawning of enemies, which is what we'll be adding in first. Let's create a variable to store our enemy.
// Our enemy to spawn public Transform enemy;
Assets\Prefabs
folder. Once we've created the prefab, we can remove the enemy object from our scene by deleting it.GameController
script by double-clicking it to go into MonoDevelop. Add the following additional variables to the component:// We want to delay our code at certain times public float timeBeforeSpawning = 1.5f; public float timeBetweenEnemies = .25f; public float timeBeforeWaves = 2.0f; public int enemiesPerWave = 10; private int currentNumberOfEnemies = 0;
We now need a function to spawn enemies; let's call it SpawnEnemies
. We don't want to spawn all the enemies at once. What we want is a steady stream of enemies to come to the player over the course of the game. However, in C#, to have a function pause the gameplay without having to stop the entire game, we need to use a coroutine that looks different from all the code that we've used so far.
Start
method, add the following line:StartCoroutine(SpawnEnemies());
A coroutine is like a function that has the ability to pause execution and continue where it left off after a period of time. By default, a coroutine is resumed on the frame after we start to yield
, but it is also possible to introduce a time delay using the WaitForSeconds
function for how long you want to wait before it's called again.
SpawnEnemies
function as follows:// Coroutine used to spawn enemies IEnumerator SpawnEnemies() { // Give the player time before we start the game yield return new WaitForSeconds(timeBeforeSpawning); // After timeBeforeSpawning has elapsed, we will enter this loop while(true) { // Don't spawn anything new until all the previous // wave's enemies are dead if(currentNumberOfEnemies <= 0) { float randDirection; float randDistance; //Spawn 10 enemies in a random position for (int i = 0; i < enemiesPerWave; i++) { // We want the enemies to be off screen // (Random.Range gives us a number between the // first and second parameter) randDistance = Random.Range(10, 25); // Enemies can come from any direction randDirection = Random.Range(0, 360); // Using the distance and direction we set the position float posX = this.transform.position.x + (Mathf.Cos((randDirection) * Mathf.Deg2Rad) * randDistance); float posY = this.transform.position.y + (Mathf.Sin((randDirection) * Mathf.Deg2Rad) * randDistance); // Spawn the enemy and increment the number of // enemies spawned // (Instantiate Makes a clone of the first parameter // and places it at the second with a rotation of // the third.) Instantiate(enemy, new Vector3 (posX, posY, 0), this.transform.rotation); currentNumberOfEnemies++; yield return new WaitForSeconds(timeBetweenEnemies); } } // How much time to wait before checking if we need to // spawn another wave yield return new WaitForSeconds(timeBeforeWaves); } }
currentNumberOfEnemies
, but it's a private variable, which means that it can only be changed inside the GameController
class or one of the methods inside of the class. Simple enough? Now let's add a new function in our GameController
class:// Allows classes outside of GameController to say when we killed // an enemy. public void KilledEnemy() { currentNumberOfEnemies--; }
EnemyBehaviour
class. Inside the OnCollisionEnter2D
function under the Destroy function call, add the following two lines:GameController controller = GameObject.FindGameObjectWithTag("GameController").GetComponent("GameController") as GameController; controller.KilledEnemy();
The preceding line gets the script GameController
with the tag GameController
. The as
keyword casts the object to a GameController
object. Casting basically tells the computer, "Even though the code says it's some class, I'm telling you that it's another one."
This will call the KilledEnemy
function from GameController
, onto which we set the GameController
tag in step 2.
We now have waves of enemies that will now move toward the player! When we kill all the enemies inside a wave, we will spawn the next wave. In such a short period of time, we already have so much going on!
Change the font size
Change margin width
Change background colour