Unity Game Development Blueprints

4.5 (2 reviews total)
By John P. Doran
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. 2D Twin-stick Shooter

About this book

Unity 3D is a powerful and popular game development tool that has taken game developers by storm. It is a cross-platform game engine, enabling you to write your game once and then port it to PCs, consoles, and even the Web, making it a great choice for both indie and AAA developers. Unity combines serious power with a friendly, easy-to-use interface.

Unity Game Development Blueprints takes you on an exciting journey where you'll learn how to use Unity to its best by building a project in 2D, then a 3D game with 2D gameplay, and finally a 3D title.

An easy-to-follow guide with each project containing step-by-step explanations, diagrams, screenshots, and downloadable materials. Concepts in Unity and C# are explained.

Publication date:
November 2014
Publisher
Packt
Pages
318
ISBN
9781783553655

 

Chapter 1. 2D Twin-stick Shooter

The shoot 'em up genre of games is one of the earliest kinds of games. In shoot 'em ups, the player character is a single entity fighting a large number of enemies. They are typically played with a top-down perspective, which is perfect for 2D games. Shoot 'em up games also exist with many categories, based upon their design elements.

Elements of a shoot 'em up were first seen in the 1961 Spacewar! game. However, the concept wasn't popularized until 1978 with Space Invaders. The genre was quite popular throughout the 1980s and 1990s and went in many different directions, including bullet hell games, such as the titles of the Touhou Project. The genre has recently gone through a resurgence in recent years with games such as Bizarre Creations' Geometry Wars: Retro Evolved, which is more famously known as a twin-stick shooter.

 

Project overview


Over the course of this chapter, we will be creating a 2D multidirectional shooter game similar to Geometry Wars.

In this game, the player controls a ship. This ship can move around the screen using the keyboard and shoot projectiles in the direction that the mouse points at. Enemies and obstacles will spawn toward the player, and the player will avoid/shoot them. This chapter will also serve as a refresher on a lot of the concepts of working in Unity and give an overview of the recent addition of native 2D tools into Unity.

Your objectives

This project will be split into a number of tasks. It will be a simple step-by-step process from beginning to end. Here is the outline of our tasks:

  • Setting up the project

  • Creating our scene

  • Adding in player movement

  • Adding in shooting functionality

  • Creating enemies

  • Adding GameController to spawn enemy waves

  • Particle systems

  • Adding in audio

  • Adding in points, score, and wave numbers

  • Publishing the game

Prerequisites

Before we start, we will need to get the latest Unity version, which you can always get by going to http://unity3d.com/unity/download/ and downloading it there:

At the time of writing this book, the version is 4.5.3, but this project should work in future versions with minimal changes.

We will also need some graphical assets for use in our project. These can be downloaded from the example code provided for this book on Packt Publishing's website (http://www.PacktPub.com).

Navigate to the preceding URL, and download the Chapter1.zip package and unzip it. Inside the Chapter1 folder, there are a number of things, including an Assets folder, which will have the art, sound, and font files you'll need for the project as well as the Chapter_1_Completed.unitypackage (this is the complete chapter package that includes the entire project for you to work with). I've also added in the complete game exported (TwinstickShooter Exported) as well as the entire project zipped up in the TwinstickShooter Project.zip file.

 

Setting up the project


At this point, I have assumed that you have Unity freshly installed and have started it up.

  1. With Unity started, go to File | New Project. Select Project Location of your choice somewhere on your hard drive, and ensure you have Setup defaults for set to 2D. Once completed, select Create. At this point, we will not need to import any packages, as we'll be making everything from scratch. It should look like the following screenshot:

  2. From there, if you see the Welcome to Unity pop up, feel free to close it out as we won't be using it. At this point, you will be brought to the general Unity layout, as follows:

Note

Again, I'm assuming you have some familiarity with Unity before reading this book; if you would like more information on the interface, please visit http://docs.unity3d.com/Documentation/Manual/LearningtheInterface.html.

Keeping your Unity project organized is incredibly important. As your project moves from a small prototype to a full game, more and more files will be introduced to your project. If you don't start organizing from the beginning, you'll keep planning to tidy it up later on, but as deadlines keep coming, things may get quite out of hand.

This organization becomes even more vital when you're working as part of a team, especially if your team is telecommuting. Differing project structures across different coders/artists/designers is an awful mess to find yourself in.

Setting up a project structure at the start and sticking to it will save you countless minutes of time in the long run and only takes a few seconds, which is what we'll be doing now. Perform the following steps:

  1. Click on the Create drop-down menu below the Project tab in the bottom-left side of the screen.

  2. From there, click on Folder, and you'll notice that a new folder has been created inside your Assets folder.

  3. After the folder is created, you can type in the name for your folder. Once done, press Enter for the folder to be created. We need to create folders for the following directories:

    • Animations

    • Prefabs

    • Scenes

    • Scripts

    • Sprites

    Note

    If you happen to create a folder inside another folder, you can simply drag-and-drop it from the left-hand side toolbar. If you need to rename a folder, simply click on it once and wait, and you'll be able to edit it again.

    You can also use Ctrl + D to duplicate a folder if it is selected.

  4. Once you're done with the aforementioned steps, your project should look something like this:

 

Creating our scene


Now that we have our project set up, let's get started with creating our player:

  1. Double-click on the Sprites folder. Once inside, go to your operating system's browser window, open up the Chapter 1/Assets folder that we provided, and drag the playerShip.png file into the folder to move it into our project. Once added, confirm that the image is Sprite by clicking on it and confirming from the Inspector tab that Texture Type is Sprite (Sprite (2D and UI) in 4.6). If it isn't, simply change it to that, and then click on the Apply button. Have a look at the following screenshot:

    Note

    If you do not want to drag-and-drop the files, you can also right-click within the folder in the Project Browser (bottom-left corner) and select Import New Asset to select a file from a folder to bring it in.

    Note

    The art assets used for this tutorial were provided by Kenney. To see more of their work, please check out www.kenney.nl.

  2. Next, drag-and-drop the ship into the Scene tab (the center part that's currently dark gray). Once completed, set the position of the sprite to the center of the Screen (0, 0) by right-clicking on the Transform component and then selecting Reset Position. Have a look at the following screenshot:

  3. Now, with the player in the world, let's add in a background. Drag-and-drop the background.png file into your Sprites folder. After that, drag-and-drop a copy into the scene.

    If you put the background on top of the ship, you'll notice that currently the background is in front of the player (Unity puts newly added objects on top of previously created ones if their position on the Z axis is the same; this is commonly referred to as the z-order), so let's fix that.

    Note

    Objects on the same Z axis without sorting layer are considered to be equal in terms of draw order; so just because a scene looks a certain way this time, when you reload the level it may look different. In order to guarantee that an object is in front of another one in 2D space is by having different Z values or using sorting layers.

  4. Select your background object, and go to the Sprite Renderer component from the Inspector tab. Under Sorting Layer, select Add Sorting Layer. After that, click on the + icon for Sorting Layers, and then give Layer 1 a name, Background. Now, create a sorting layer for Foreground and GUI. Have a look at the following screenshot:

  5. Now, place the player ship on the foreground and the background by selecting the object once again and then setting the Sorting Layer property via the drop-down menu. Now, if you play the game, you'll see that the ship is in front of the background, as follows:

    At this point, we can just duplicate our background a number of times to create our full background by selecting the object in the Hierarchy, but that is tedious and time-consuming. Instead, we can create all the duplicates by either using code or creating a tileable texture. For our purposes, we'll just create a texture.

  6. Delete the background sprite by left-clicking on the background object in the Hierarchy tab on the left-hand side and then pressing the Delete key. Then select the background sprite in the Project tab, change Texture Type in the Inspector tab to Texture, and click on Apply.

  7. Now let's create a 3D cube by selecting Game Object | Create Other | Cube from the top toolbar. Change the object's name from Cube to Background. In the Transform component, change the Position to (0, 0, 1) and the Scale to (100, 100, 1).

    Note

    If you are using Unity 4.6 you will need to go to Game Object | 3D Object | Cube to create the cube.

    Since our camera is at 0, 0, -10 and the player is at 0, 0, 0, putting the object at position 0, 0, 1 will put it behind all of our sprites. By creating a 3D object and scaling it, we are making it really large, much larger than the player's monitor. If we scaled a sprite, it would be one really large image with pixelation, which would look really bad. By using a 3D object, the texture that is applied to the faces of the 3D object is repeated, and since the image is tileable, it looks like one big continuous image.

  8. Remove Box Collider by right-clicking on it and selecting Remove Component.

  9. Next, we will need to create a material for our background to use. To do so, under the Project tab, select Create | Material, and name the material as BackgroundMaterial. Under the Shader property, click on the drop-down menu, and select Unlit | Texture. Click on the Texture box on the right-hand side, and select the background texture. Once completed, set the Tiling property's x and y to 25. Have a look at the following screenshot:

    Note

    In addition to just selecting from the menu, you can also drag-and-drop the background texture directly onto the Texture box, and it will set the property.

    Tiling tells Unity how many times the image should repeat in the x and y positions, respectively.

  10. Finally, go back to the Background object in Hierarchy. Under the Mesh Renderer component, open up Materials by left-clicking on the arrow, and change Element 0 to our BackgroundMaterial material. Consider the following screenshot:

Now, when we play the game, you'll see that we now have a complete background that tiles properly.

Scripting 101

In Unity, the behavior of game objects is controlled by the different components that are attached to them in a form of association called composition. These components are things that we can add and remove at any time to create much more complex objects. If you want to do anything that isn't already provided by Unity, you'll have to write it on your own through a process we call scripting. Scripting is an essential element in all but the simplest of video games.

Unity allows you to code in either C#, Boo, or UnityScript, a language designed specifically for use with Unity and modelled after JavaScript. For this book, we will use C#.

C# is an object-oriented programming language—an industry-standard language similar to Java or C++. The majority of plugins from Asset Store are written in C#, and code written in C# can port to other platforms, such as mobile, with very minimal code changes. C# is also a strongly-typed language, which means that if there is any issue with the code, it will be identified within Unity and will stop you from running the game until it's fixed. This may seem like a hindrance, but when working with code, I very much prefer to write correct code and solve problems before they escalate to something much worse.

 

Implementing player movement


Now, at this point, we have a great-looking game, but nothing at all happens. Let's change that now using our player. Perform the following steps:

  1. Right-click on the Scripts folder you created earlier, click on Create, and select the C# Script label. Once you click on it, a script will appear in the Scripts folder, and it should already have focus and should be asking you to type a name for the script—call it PlayerBehaviour.

  2. Double-click on the script in Unity, and it will open MonoDevelop, which is an open source integrated development environment (IDE) that is included with your Unity installation.

After MonoDevelop has loaded, you will be presented with the C# stub code that was created automatically for you by Unity when you created the C# script.

Let's break down what's currently there before we replace some of it with new code. At the top, you will see two lines:

using UnityEngine;
using System.Collections;

Tip

Downloading the example code

You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The engine knows that if we refer to a class that isn't located inside this file, then it has to reference the files within these namespaces for the referenced class before giving an error. We are currently using two namespaces.

The UnityEngine namespace contains interfaces and class definitions that let MonoDevelop know about all the addressable objects inside Unity.

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables, and dictionaries.

We will be using a list, so we will change the line to the following:

using System.Collections.Generic;

The next line you'll see is:

public class PlayerBehaviour : MonoBehaviour {

You can think of a class as a kind of blueprint for creating a new component type that can be attached to GameObjects, the objects inside our scenes that start out with just a Transform and then have components added to them. When Unity created our C# stub code, it took care of that; we can see the result, as our file is called PlayerBehaviour and the class is also called PlayerBehaviour. Make sure that your .cs file and the name of the class match, as they must be the same to enable the script component to be attached to a game object. Next up is the: MonoBehaviour section of the code. The : symbol signifies that we inherit from a particular class; in this case, we'll use MonoBehaviour. All behavior scripts must inherit from MonoBehaviour directly or indirectly by being derived from it.

Inheritance is the idea of having an object to be based on another object or class using the same implementation. With this in mind, all the functions and variables that existed inside the MonoBehaviour class will also exist in the PlayerBehaviour class, because PlayerBehaviour is MonoBehaviour.

For more information on the MonoBehaviour class and all the functions and properties it has, check out http://docs.unity3d.com/ScriptReference/MonoBehaviour.html. Directly after this line, we will want to add some variables to help us with the project. Variables are pieces of data that we wish to hold on to for one reason or another, typically because they will change over the course of a program, and we will do different things based on their values.

Add the following code under the class definition:

// Movement modifier applied to directional movement.
public float playerSpeed = 2.0f;

// What the current speed of our player is
private float currentSpeed = 0.0f;

/*
 * Allows us to have multiple inputs and supports keyboard, 
 * joystick, etc.
 */
public List<KeyCode> upButton;
public List<KeyCode> downButton;
public List<KeyCode> leftButton;
public List<KeyCode> rightButton;

// The last movement that we've made
private Vector3 lastMovement = new Vector3();

Between the variable definitions, you will notice comments to explain what each variable is and how we'll use it. To write a comment, you can simply add a // to the beginning of a line and everything after that is commented upon so that the compiler/interpreter won't see it. If you want to write something that is longer than one line, you can use /* to start a comment, and everything inside will be commented until you write */ to close it. It's always a good idea to do this in your own coding endeavors for anything that doesn't make sense at first glance.

Note

For those of you working on your own projects in teams, there is an additional form of commenting that Unity supports, which may make your life much nicer: XML comments. They take up more space than the comments we are using, but also document your code for you. For a nice tutorial about that, check out http://unitypatterns.com/xml-comments/.

In our game, the player may want to move up using either the arrow keys or the W key. You may even want to use something else. Rather than restricting the player to just having one button, we will store all the possible ways to go up, down, left, or right in their own container. To do this, we are going to use a list, which is a holder for multiple objects that we can add or remove while the game is being played.

Note

For more information on lists, check out http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

One of the things you'll notice is the public and private keywords before the variable type. These are access modifiers that dictate who can and cannot use these variables. The public keyword means that any other class can access that property, while private means that only this class will be able to access this variable. Here, currentSpeed is private because we want our current speed not to be modified or set anywhere else. But, you'll notice something interesting with the public variables that we've created. Save your script by pressing Ctrl + S and then go back into the Unity project and drag-and-drop the PlayerBehaviour script onto the playerShip object. Before going back to the Unity project though, make sure that you save your PlayerBehaviour script. Not saving is a very common mistake made by people working with MonoDevelop. Have a look at the following screenshot:

You'll notice now that the public variables that we created are located inside Inspector for the component. This means that we can actually set those variables inside Inspector without having to modify the code, allowing us to tweak values in our code very easily, which is a godsend for many game designers. You may also notice that the names have changed to be more readable. This is because of the naming convention that we are using with each word starting with a capital letter. This convention is called CamelCase (more specifically headlessCamelCase).

Now change the Size of each of the Button variables to 2, and fill in the Element 0 value with the appropriate arrow and Element 1 with W for up, A for left, S for down, and D for right. When this is done, it should look something like the following screenshot:

Now that we have our variables set, go back to MonoDevelop for us to work on the script some more.

The line after that is a function definition for a method called Start; it isn't a user method but one that belongs to MonoBehaviour. Where variables are data, functions are the things that modify and/or use that data. Functions are self-contained modules of code (enclosed within braces, { and }) that accomplish a certain task. The nice thing about using a function is that once a function is written, it can be used over and over again. Functions can be called from inside other functions:

void Start () {
    
}

Start is only called once in the lifetime of the behavior when the game starts and is typically used to initialize data.

Note

If you're used to other programming languages, you may be surprised that initialization of an object is not done using a constructor function. This is because the construction of objects is handled by the editor and does not take place at the start of gameplay as you might expect. If you attempt to define a constructor for a script component, it will interfere with the normal operation of Unity and can cause major problems with the project.

However, for this behavior, we will not need to use the Start function. Perform the following steps:

  1. Delete the Start function and its contents.

    The next function that we see included is the Update function. Also inherited from MonoBehaviour, this function is called for every frame that the component exists in and for each object that it's attached to. We want to update our player ship's rotation and movement every turn.

  2. Inside the Update function (between { and }), put the following lines of code:

    // Rotate player to face mouse
    Rotation();
    // Move the player's body
    Movement();

    Here, I called two functions, but these functions do not exist, because we haven't created them yet, which is why the text shows up as Red inside of MonoDevelop. Let's do that now!

  3. Below the Update function and before } that closes the class at the end of the file, put the following function to close the class:

    // Will rotate the ship to face the mouse.
    void Rotation()
    {
      // We need to tell where the mouse is relative to the 
      // player
      Vector3 worldPos = Input.mousePosition;
      worldPos = Camera.main.ScreenToWorldPoint(worldPos);
    
      /*
       * Get the differences from each axis (stands for 
       * deltaX and deltaY)
       */
      float dx = this.transform.position.x - worldPos.x;
      float dy = this.transform.position.y - worldPos.y;
    
      // Get the angle between the two objects
      float angle = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg;
    
      /* 
        * The transform's rotation property uses a Quaternion, 
        * so we need to convert the angle in a Vector 
        * (The Z axis is for rotation for 2D).
      */
      Quaternion rot = Quaternion.Euler(new Vector3(0, 0, angle + 90));
    
      // Assign the ship's rotation
      this.transform.rotation = rot;
    }

    Now if you comment out the Movement line and run the game, you'll notice that the ship will rotate in the direction in which the mouse is. Have a look at the following screenshot:

  4. Below the Rotation function, we now need to add in our Movement function the following code. Uncomment the Movement function call if you commented it out earlier:

    // Will move the player based off of keys pressed
    void Movement()
    {
      // The movement that needs to occur this frame
      Vector3 movement = new Vector3();
    
      // Check for input
      movement += MoveIfPressed(upButton, Vector3.up);
      movement += MoveIfPressed(downButton, Vector3.down);
      movement += MoveIfPressed(leftButton, Vector3.left);
      movement += MoveIfPressed(rightButton, Vector3.right);
    
      /* 
        * If we pressed multiple buttons, make sure we're only 
        * moving the same length.
      */
      movement.Normalize ();
    
      // Check if we pressed anything
      if(movement.magnitude > 0)
      {
        // If we did, move in that direction
        currentSpeed = playerSpeed;
        this.transform.Translate(movement * Time.deltaTime * playerSpeed, Space.World);
        lastMovement = movement;
      }
      else
      {
        // Otherwise, move in the direction we were going
        this.transform.Translate(lastMovement * Time.deltaTime * currentSpeed, Space.World);
        // Slow down over time
        currentSpeed *= .9f;
      }
    }

    Now inside this function I've created another function called MoveIfPressed, so we'll need to add that in as well.

  5. Below this function, add in the following function as well:

    /* 
      * Will return the movement if any of the keys are pressed,
      * otherwise it will return (0,0,0)
    */
    Vector3 MoveIfPressed( List<KeyCode> keyList, Vector3 Movement)
    {
      // Check each key in our list
      foreach (KeyCode element in keyList)
      {
        if(Input.GetKey(element))
        {
          /*
            * It was pressed so we leave the function 
            * with the movement applied.
          */
          return Movement;
        }
      }
    
      // None of the keys were pressed, so don't need to move
      return Vector3.zero;
    }
  6. Now, save your file and move back into Unity. Save your current scene as Chapter_1.unity by going to File | Save Scene. Make sure to save the scene to our Scenes folder we created earlier.

  7. Run the game by pressing the play button. Have a look at the following screenshot:

Now you'll see that we can move using the arrow keys or the W A S D keys, and our ship will rotate to face the mouse. Great!

 

Shooting behavior


The next thing we will do is give our player the ability to shoot:

  1. Open up the PlayerBehaviour script. In the top section where the other variables are present, we need to add some additional ones that we'll use:

    // The laser we will be shooting
    public Transform laser;
    
    // How far from the center of the ship should the laser be
    public float laserDistance = .2f;
    
    // How much time (in seconds) we should wait before 
    // we can fire again
    public   float   timeBetweenFires = .3f;
    
    // If value is less than or equal 0, we can fire
    private float timeTilNextFire = 0.0f;
    
    // The buttons that we can use to shoot lasers
    public List<KeyCode> shootButton;

    One thing you may have noticed is that we have a laser variable that is of the type Transform. This is the laser we'll fire, which we will create shortly.

  2. Inside our Update function, we will need to add some additional code, which is as follows:

    // a foreach loop will go through each item inside of 
    // shootButton and do whatever we placed in {}s using the 
    // element variable to hold the item
    foreach (KeyCode element in shootButton)
    {
      if(Input.GetKey(element) && timeTilNextFire < 0)
      {
        timeTilNextFire = timeBetweenFires;
        ShootLaser();
        break;
      }
    }
    
    timeTilNextFire -= Time.deltaTime;

    In a manner very similar to what we did before with the player movement, we check each of the keys we allow the player to shoot with (such as the spacebar and Enter keys). If they press any of these keys and can fire again, then we will reset our timer and shoot a laser. However, we haven't made the ShootLaser function. Let's do that now.

  3. Underneath the functions, add the following function:

    // Creates a laser and gives it an initial position in front 
    // of the ship.    
    void ShootLaser()
    {
      // calculate the position right in front of the ship's
      // position lazerDistance units away
      float posX = this.transform.position.x + 
                   (Mathf.Cos((transform.localEulerAngles.z - 90) * Mathf.Deg2Rad) * -laserDistance);
      float posY = this.transform.position.y + (Mathf.Sin((transform.localEulerAngles.z - 90) * Mathf.Deg2Rad) * -laserDistance);
      
      Instantiate(laser, new Vector3 (posX, posY, 0), this.transform.rotation);
    }
  4. Save your file, and go back into Unity. You'll now see a number of additional variables that we can now set. Be sure to set the Shoot Button variable in the same manner that we did the movement buttons, changing the Size to 2 and setting Element 0 to Mouse0 and Element 1 to Space.

    Note

    If, for some reason, your Inspector window doesn't update, save your project, and restart Unity. Upon reset, it should be updated.

  5. Next, we will need to create our laser. Go back into our Assets folder from the example code, and move the laser.png file into our Project tab's Sprites folder.

  6. Following that, drag-and-drop it into your scene from the Scene tab to place it in the level.

  7. Right-click the Scripts folder you created earlier, click on Create, and select the C# Script label. Call this new script LaserBehaviour. Go into MonoDevelop, and use the following code:

    using UnityEngine;
    using System.Collections;
    
    public class LaserBehaviour : MonoBehaviour 
    {
      // How long the laser will live
      public float lifetime = 2.0f;
    
      // How fast will the laser move
      public float speed = 5.0f;
    
    
      // How much damage will this laser do if we hit an enemy
      public int damage = 1;
    
      // Use this for initialization
      void Start () 
      {
        // The game object that contains this component will be
        // destroyed after lifetime seconds have passed
        Destroy(gameObject, lifetime);
      }
      
      // Update is called once per frame
      void Update () 
      {
        transform.Translate(Vector3.up * Time.deltaTime * speed);
      }
    }
  8. Attach LaserBehaviour to the laser object. Finally, add a Box Collider component by first selecting the laser object and then going to Component | Physics 2D | Box Collider 2D. The collision box, by default, will be the size of the image, but I want to shrink it to fit what is visually seen of it. To do that, we will change the Size attribute's X property to .06 and Y to .5.

    Now, the laser will move in the direction that it's facing and die after a period of 2 seconds! Next, let's make it so that the player can shoot them.

  9. In the Project tab, go to the Assets | Prefabs folder, and drag-and-drop the laser object from our Hierarchy tab into it. You'll notice that the object Hierarchy will turn blue to show that it is a prefab.

    Note

    Prefabs or prefabricated objects are the objects that we set aside to make copies during runtime, such as our bullets and eventually enemies that we'll spawn into the world, and we can create as many as we want. When you add a prefab to a scene, you create an instance of it. All of these instances are clones of the object located in our Assets. Whenever you change something in the prefab located in our Prefab folder, those changes are applied to all the objects that are already inside of your scene. For example, if you add a new component to Prefab, all the other objects we have in the scene will instantly contain the component as well. We can also apply any of the ones in our scene to be the blueprint for the others as well, which we will do later on. However, it is also possible to change the properties of a single instance while keeping the link intact. Simply change any property of a prefab instance inside your scene, and that particular value will become bolded to show that the value is overridden, and they will not be affected by changes in the source prefab. This allows you to modify prefab instances to make them different (unique) from their source prefabs without breaking the prefab link.

    Have a look at the following screenshot:

  10. Now, delete the laser object from our scene, and then go to the playerShip object. Drag-and-drop the laser prefab into the Laser property of the PlayerBehavior component.

  11. Finally, add a circle collider to our ship by going to Component | Physics 2D | Circle Collider 2D. Change the Radius property to .3.

Generally, in games, we want to be as efficient as possible toward calculations. Polygon collision is the most accurate collision, but it is much slower than using a box or a circle. In this case, I wanted to use a circle, because not only is it more efficient but it also allows the player some leeway in how close they can get to enemies without being hurt. Players will always think it's their skill if they get away, but if the collider is too big they will think the game is broken, which we want to avoid.

Have a look at the following screenshot:

Now, our ship can shoot in the direction that the mouse is currently facing.

 

Creating enemies


Now, it's really cool that we have a player, but it'll get really boring if all we can do is move around and shoot some lasers in the dark. Next, we'll introduce some simple enemies that will move toward the player that we'll be able to shoot later. Perform the following steps:

  1. Exit the game and access our example code's Assets folder; move the enemy.png file into our Sprites folder.

  2. Following that, drag-and-drop it into your scene from the Scene tab to place it in the level.

  3. Right-click on the Scripts folder you created earlier, click on Create, and select the C# Script label. Call this new script MoveTowardsPlayer. Go to MonoDevelop and use the following code:

    using UnityEngine;
    using System.Collections;
    
    public class MoveTowardsPlayer : MonoBehaviour 
    {
      private Transform player;
      public float speed = 2.0f;
    
      // Use this for initialization
      void Start () 
      {
        player = GameObject.Find("playerShip").transform;
      }
      
      // Update is called once per frame
      void Update () 
      {
        Vector3 delta = player.position - transform.position;
        delta.Normalize();
        float moveSpeed = speed * Time.deltaTime;
        transform.position = transform.position + (delta * moveSpeed);
      }
    }

    In the beginning of the game, I find the player ship and get his transform component. Then, in every frame of the project, we move the enemy from where it currently is to the direction where the player is at.

    Note

    If you ever want to have objects run away from the player, use a negative value for the speed variable.

  4. Drag-and-drop this newly added behavior onto our enemy object.

  5. Next, add a circle collider to our enemy by going to Component | Physics 2D | Circle Collider 2D. Change the Radius property to .455, and run the game. Have a look at the following screenshot:

Now, you'll see that the enemy will always move toward you! But if we shoot it, nothing happens. Let's fix that as follows.

  1. Right-click on the Scripts folder you created earlier, click on Create, and select the C# Script label. Call this new script EnemyBehaviour. Go to MonoDevelop, and use the following code:

    using UnityEngine; // MonoBehaviour
    
    public class EnemyBehaviour : MonoBehaviour 
    {
    
      // How many times should I be hit before I die
      public int health = 2;
    
      void OnCollisionEnter2D(Collision2D theCollision)
      {
        // Uncomment this line to check for collision
        //Debug.Log("Hit"+ theCollision.gameObject.name);
    
        // this line looks for "laser" in the names of 
        // anything collided.
        if(theCollision.gameObject.name.Contains("laser"))
        {
          LaserBehaviour laser = theCollision.gameObject.GetComponent("LaserBehaviour") as LaserBehaviour;
          health -= laser.damage;
          Destroy (theCollision.gameObject);
        }
        
        if (health <= 0) 
        {
          Destroy (this.gameObject);
        }
      }
    }

    Now, you will notice that we have commented a line of code calling the function Debug.Log. This function will print something onto your console, which may help you when debugging your own code in the future.

  2. Save the file, and then go back into Unity. Attach the EnemyBehaviour behavior to your enemy object. For collision events to register we need to add a Rigidbody 2D component to our enemy by going to Component | Physics 2D | Rigidbody 2D. Change the Gravity Scale to 0 so it will not fall. Have a look at the following screenshot:

    Note

    OnCollisionEnter2D is a function that will trigger when two objects with 2D colliders collide. It is important to note collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached (which we just did).

    For more info on OnCollisionEnter2D check out http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html.

Now, whenever we hit the enemy with our bullets twice, it will die. Nice!

 

Adding GameController to spawn enemy waves


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:

  1. First, create an empty game object by going to GameObject | Create Empty. From there, with the object selected, go to Inspector and set its name to GameController, and optionally, for neatness sake, set its Position to (0, 0, 0).

  2. Underneath the name, you'll see the Tag property. Change it from Untagged to GameController.

    Note

    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.

  3. Next, select Add Component | New Script, and once you are brought to the next menu, change the language to C# (C Sharp), and set the name of the script to GameController. Then press Enter or click on Create and Add.

  4. Select the newly created script in the 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.

  5. Inside of the class definition, add the following variable and then save the file:

    // Our enemy to spawn
    public Transform enemy;
  6. Now, we can set the enemy that we currently have in the scene, but we should instead make the enemy a prefab and use it. To do so, drag the enemy from Hierarchy into your Assets\Prefabs folder. Once we've created the prefab, we can remove the enemy object from our scene by deleting it.

  7. Next, drag-and-drop the enemy prefab into the Enemy variable in the GameController component.

  8. Next, go back into our 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.

  9. Inside the Start method, add the following line:

    StartCoroutine(SpawnEnemies());

    Note

    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.

  10. Now that we are already using the function, let's add in the 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);
      }
    }
  11. Now, when we destroy an enemy, we want to decrement the number of 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--;
    }
  12. Finally, let's go back into our 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.

  13. With all those changes, save both script files and run the game! Have a look at the following screenshot:

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!

 

Particle systems for enemy explosion


Now that we have the basis for our game, let's spend some time to make the project look nicer. Particle systems are one of my go-to things to add juiciness to a game project and helps to set your project apart from others. Particles systems are composed of two separate parts: a particle and the thing that emits it. A particle is a small object that stores properties; generally we try to make these objects as simple as possible, as we want to spawn a large number of them. The emitter's job is to spawn a number of these and initialize their properties. Thankfully, Unity has a fully featured particle editor that's included with the engine, and we're going to use it in this section. Perform the following steps:

  1. Create a new particle system by going to GameObject | Create Other | Particle System. Have a look at the following screenshot:

    Note

    If you are using Unity 4.6 use GameObject | Particle System to create the particle system.

    Once you do this, you should see a default particle system show up. Do note that the system will only animate if it is the object selected and Unity is the active window.

  2. Change the object's name to Explosion. First, under the Particle System tab, change Duration to 1.00, and then uncheck Looping.

  3. Click on the downwards-facing arrow on the right-hand side of Start Lifetime, and change the values to be Random Between Two Constants. Change those values to 0 and 1. Do the same with Start Speed. Make Start Size use random values between 0 and .5.

  4. Next, we will set the object's Start Color value to the same color as the UFO ship (you can use the eyedropper tool or set it to 210, 224, 230) and an Alpha value of 125. Have a look at the following screenshot:

  5. Open the Emission tab, and change Rate to 200. This is how many particles are spawned at a time.

  6. Open the Shape tab, change the Shape property to Sphere, and then set Radius to .35 to fit the rim of the ship. Enable the Random Direction option.

  7. Back in the Explosion section, change the Simulation Space to World; that way, if this object moves, the already-spawned particles will not move.

  8. Now, make this object a prefab by dragging-and-dropping it into the Prefabs folder. After that, delete the Explosion object in your Hierarchy object.

  9. Go back to your EnemyBehaviour script file. We will first want to add in a new variable for us to use to spawn this explosion when it dies:

    // When the enemy dies, we play an explosion
    public Transform explosion;
  10. Back in Inspector, drag-and-drop your new explosion prefab to fill in the explosion variable slot in our enemy prefab.

  11. Coming back to the EnemyBehaviour script, let's spawn an explosion whenever we die. Inside your if(health <= 0) section of CollisionEnter2D, add in the following lines:

    // Check if explosion was set
    if(explosion)
    {
      GameObject exploder = ((Transform)Instantiate(explosion, this.transform.position, this.transform.rotation)).gameObject;
      Destroy(exploder, 2.0f);
    }
  12. Save your script and scene files, and run your project! Have a look at the following screenshot:

And now, whenever an enemy dies, it will spawn an explosion for us to see!

 

Adding in sound effects/music


Another thing that we can do to give the project a little more polish is add in sound effects and background music. Let's do that now. Perform the following steps:

  1. Select your enemy prefab, and add an Audio Source component to it by selecting Component | Audio | Audio Source. An audio source lets the audio listener into Main Camera knowing that this is an object that can play sounds. Once you create the Audio Source, open the 3D Sound Settings and change the Min Distance of the Volume Rolloff to 10. Unity will attempt to alter the volume and pan sounds to make the project sound nicer, but for this project, it just makes everything much softer, so we're going to undo the effect unless it's far away.

  2. After this, let's go into our EnemyBehaviour script! As usual, we'll need to add in a new variable for us to use to play whenever we're hit:

    // What sound to play when we're hit 
    public AudioClip hitSound;
  3. Next, go into the CollisionEnter2D function. After the Destroy(theCollision.gameObject) line, add the following code:

    // Plays a sound from this object's AudioSource
    audio.PlayOneShot(hitSound);

    Note

    For more information about the PlayOneShot function, check out http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html.

    For more information on the Audio Source component (audio), check out http://docs.unity3d.com/ScriptReference/AudioSource.html.

Now, we need some actual sounds to play. I've set aside a folder of assets for you in the Example Code folder, so drag-and-drop the Sounds folder into your project's Assets folder:

  1. Back in the inspector for our enemy, let's set the Hit Sound variable in the EnemyBehaviour script to the hit sound that we've imported by using drag-and-drop. Now if we play the game, when we hit an enemy, the sound will be played! Now, let's have a sound if we destroy the enemy!

  2. Go to the Explosion prefab, and add an Audio Source component in the same way we did in step 1. Once you create the audio source, open up the 3D Sound Settings, and change the Min Distance of the Volume Rolloff to 10. After this, set the Audio Clip property in the component to the explode sound.

  3. Now, going back to our EnemyBehaviour script, go to the line after we instantiate the exploder object, and add the following line before we destroy the exploder:

    exploder.audio.Play();

    Now, if you play the game, hitting the object will play one sound, and when the object is destroyed, the explosion will play a sound. Because the sound is in the Audio Clip property, we can just call the Play function. However, if you want an object to play multiple sounds, it's better to have separate AudioClip variables just as we did with EnemyBehaviour.

  4. Finally, I want to play a sound whenever we fire a shot. To do that, let's go to playerShip and add an audio source. Once you create the audio source, open 3D Sound Settings, and change the Min Distance of Volume Rolloff to 10.

  5. Next, go into PlayerBehaviour, and add in a new variable, as follows:

    // What sound to play when we're shooting
    public AudioClip shootSound;
  6. After this, whenever we shoot a bullet, let's play the new sound at the beginning of the ShootLasers function:

    audio.PlayOneShot(shootSound);
  7. Coming back to Inspector, set the Shoot Sound property in the PlayerBehaviour component to the shoot sound effect.

  8. Finally, let's add in our background music. Go to your Main Camera object in Hierarchy. Add an Audio Source component. There's no need to set Min Distance in this case, because this object is where the audio listener is. Change Audio Clip to bgm, check the Loop option, and set the Volume to .25.

    Note

    The background music is provided for this project by Stratkat (Kyle Smith). If you are interested in more of his work, check out his website at http://daydreamanatomy.bandcamp.com/.

  9. Save everything, and run the game!

While we won't see any changes at this point for those of you actually running the game, you'll notice quite a change when the game is started. It's already feeling much more like a game.

Note

If you don't want to deal with the 3D settings, you can also select the sound files and uncheck the 3D sound option, but this will give you less control.

 

Adding in points, score, and wave numbers


One of the most important things to do in a game is reward the player and give them a sense of progression. For now, let's reward the player with a score we will display for them and also let the player know exactly which wave he is on. Perform the following steps:

  1. Create a new GUI Text object by going to the Hierarchy tab and selecting Create | GUI Text.

    Note

    If you are using the Unity 4.6 beta or higher versions, instead of creating the object in this way, you will need to select GameObject | Create Empty to create an empty object. Once you've done that with the object selected, then click on Component | Rendering | GUIText to add the component there.

  2. After this, switch to the Game tab, as you will be unable to see GUI Text in the Scene view. Note that the game should still not be started as yet, so don't hit the Play button. Have a look at the following screenshot:

  3. Change the GUI Text object's name to Score Counter. Under the GUI Text component, change the Text to Score: 0. After that, change the Position of Transform to (0, 1, 0) to put it on the left-hand side of the screen.

    GUI elements are placed in viewport space, which means that the space of the GUI is a value from (0,0) in the bottom-left corner to (1,1) in the top-right corner.

  4. Though this is technically fine, it would be nice to have some space so that the text isn't completely off the screen. To give us a bit of padding, let's set the Pixel Offset property to (10, -10) to move it 10 pixels toward the right-hand side and 10 pixels down. Note that Pixel Offset uses pixel space. Have a look at the following screenshot:

  5. Now that we have the text set up, let's set the font. Drag-and-drop the Font folder into your project. Then set Font to OSP-DIN and Font Size to 25.

    Note

    The font used in this project was created by the OSP Foundry. For more information about their stuff check out http://ospublish.constantvzw.org/foundry/.

  6. Next, duplicate the Score Counter object by right-clicking and selecting Duplicate. Set this duplicate's name to Waves Counter, and change its text to Wave: 0.

  7. Set the Waves Counter object's Position to (1, 1, 0). Then set Anchor to upper right and Pixel Offset to (-10, -10). Have a look at the following screenshot:

  8. Now that we have our text files created, let's now have them function correctly! First, let's go into the GameController class. Inside, we need to create some new variables as follows:

    // The values we'll be printing
    private int score = 0;
    private int waveNumber = 0;
    
    // The actual GUI text
    public GUIText scoreText;
    public GUIText waveText;
  9. Next, we will need to add a function to call whenever our score increases, as follows:

    public void IncreaseScore(int increase)
    {
      score += increase;
      scoreText.text = "Score: " + score;
    }

    The += operator will take the current value and add the right-hand side's value to it.

  10. Then, we'll need to call this function inside our EnemyBehaviour component. After the controller.KilledEnemy() line, add the following line:

    controller.IncreaseScore(10);
  11. Finally, whenever we increase the wave number we need to change its text as well. Back in the GameController class after the if(currentNumberOfEnemies <= 0) line, add the following lines:

    waveNumber++;
    waveText.text = "Wave: " + waveNumber;

    The ++ operator will take the current value of a number and increment it by 1.

  12. Save all the script files, go back to Inspector, and set the Score Text and Wave Text objects to the proper variables. After that, run the game. Have a look at the following screenshot:

And with that, you can see that everything is working together, killing enemies rewards points, and killing all the enemies in a wave triggers the next wave to start!

 

Publishing the game


The final thing that we are going to touch on for the project is actually publishing it:

  1. Go to File | Build Settings. From here, you can decide which platforms and/or scenes to include with your project.

  2. Click on the Add Current… button to add our current scene to the game, as follows:

  3. After that, since we are just publishing to our current platform, confirm that the settings are correct, and click on the Build and Run button.

  4. Once you press the button, you'll be brought to a menu to name your application that you are going to save. Give it a name, save it, and wait. If all goes well, you should be brought to a menu allowing you to set some options before the game starts:

  5. After that, click on the Play! button to see your completed project. Have a look at the following screenshot:

 

Summary


And there we have it! Within this first chapter, you've already completed an entire game project and learned how to publish it. Moving on, in the next chapter, we will tackle more advanced game types, learn additional things about Unity, and do more to push Unity to do as much as possible.

 

Challenges


For those of you who want to do more with this project, there are still plenty of things you can do, especially after finishing the rest of this book. Here are some ideas to get your mind thinking:

  • Add in feedback whenever the player hits an enemy—perhaps an animation or a change of sprites.

  • Give the player lives, and each time he is hit by an enemy, have him lose one life. Add a GUI Text to display lives as well.

  • Once you learn how to use a Game State Manager, create a main menu, pause screen, and restart button.

  • Add in Xbox control and mobile touch support.

  • As a learning experiment, convert the player shooting behaviour code to use co-routines to enable/disable being able to shoot. Discuss the advantages/disadvantages towards this method.

About the Author

  • John P. Doran

    John P. Doran is a passionate and seasoned technical game designer, software engineer, and author based in Peoria, Illinois.

    For over a decade, John has gained extensive hands-on expertise in game development, working in a variety of roles, ranging from game designer to lead UI programmer. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development.

    John is currently an instructor in residence at Bradley University. Prior to his present ventures, he was an award-winning videographer.

    Browse publications by this author

Latest Reviews

(2 reviews total)
Excellent
Good
Book Title
Unlock this full book FREE 10 day trial
Start Free Trial