Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Unity Game Development Blueprints
Unity Game Development Blueprints

Unity Game Development Blueprints: Explore the various enticing features of Unity and learn how to develop awesome games

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Unity Game Development Blueprints

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:

Prerequisites

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:
    Setting up the project
  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:
    Setting up the project

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:
    Setting up the project

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.

    Creating our scene

    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:
    Creating our scene
  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:
    Creating our scene
  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:
    Creating our scene

    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:
    Creating our scene

    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:
    Creating our scene

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:

Implementing player movement

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:

Implementing player movement

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:

    Implementing player movement
  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:
    Implementing player movement

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:

    Shooting behavior
  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:

Shooting behavior

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:
    Creating enemies

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:
    Creating enemies

    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.
    Adding GameController to spawn enemy waves
  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:
    Adding GameController to spawn enemy waves

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:
    Particle systems for enemy explosion

    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:
    Particle systems for enemy explosion
  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:
    Particle systems for enemy explosion

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:
    Adding in points, score, and wave numbers
  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:
    Adding in points, score, and wave numbers
  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:
    Adding in points, score, and wave numbers
  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:
    Adding in points, score, and wave numbers

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:
    Publishing the game
  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:
    Publishing the game
  5. After that, click on the Play! button to see your completed project. Have a look at the following screenshot:
    Publishing the game

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.
Left arrow icon Right arrow icon

Description

If you want to build enticing projects with Unity, this book is for you. Readers who are familiar with the basics of how to create simple projects in Unity will have an easier time.
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 11, 2014
Length: 318 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553655
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Nov 11, 2014
Length: 318 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553655
Vendor :
Unity Technologies
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 73.98
Unity Game Development Scripting
€36.99
Unity Game Development Blueprints
€36.99
Total 73.98 Stars icon

Table of Contents

10 Chapters
1. 2D Twin-stick Shooter Chevron down icon Chevron up icon
2. Creating GUIs Chevron down icon Chevron up icon
3. Side-scrolling Platformer Chevron down icon Chevron up icon
4. First Person Shooter Part 1 – Creating Exterior Environments Chevron down icon Chevron up icon
5. First Person Shooter Part 2 – Creating Interior Environments Chevron down icon Chevron up icon
6. First Person Shooter Part 3 – Implementing Gameplay and AI Chevron down icon Chevron up icon
7. Creating Save Files in Unity Chevron down icon Chevron up icon
8. Finishing Touches Chevron down icon Chevron up icon
9. Creating GUIs Part 2 – Unity's New GUI System Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(6 Ratings)
5 star 50%
4 star 33.3%
3 star 0%
2 star 0%
1 star 16.7%
Filter icon Filter
Top Reviews

Filter reviews by




MS HURST Dec 05, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had already watched a lot of the little tutorials explaining various concepts in Unity on the official website and although they were good, not using them in an actual project meant I didn't get a chance to see how things all work together. That is where this book comes in.The book comprises of 3 main projects (Demo videos of which are available on youtube from the author, well worth a look if you want to see what you will be making). These projects are:1) 2D Shooter (Essentially a geometry-wars type game)2) 2D Platformer using 3D assets and physics3) 3D First person "horror" gameThere are also chapters on useful concepts such as GUI (You are shown how to make a main menu, pause menu and options screen for your first project), a 2D level editor for the second project and saving and loading progress for the thirdThe projects are excellent in that they give you enough information to make a basic playable game, and then task you with adding some extra features to it, such as adding in controller capabilities for the first projectA huge bonus with this book is that it also includes a chapter on the new GUI system introduced in the Unity 4.6 beta. It is the final chapter in the book and as such doesn't have to be used if you're not using the beta version (The proper GUI chapter still uses the old system) but very handy for people who want to get ahead of the curve with the new featuresAll in all I would hugely recommend this book both to people who have opened Unity once and been completely baffled and people who have a basic understanding and need to take it to the next level in order to create full projects
Amazon Verified review Amazon
steve Nov 08, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent way to learn how to build and code games in Unity.
Amazon Verified review Amazon
Thom Dec 21, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
After reading many, many, many books about Unity development, I honestly believe THIS IS THE BEST one!It is complete yet guides you through carefully; several projects from 2D side-scroller to a fully-implemented First Person Shooter.Along the way, details on GUI menus, Environments, and other sophisticated topics are introduced appropriately.
Amazon Verified review Amazon
Chev Dec 18, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book has a nice pace for beginners, it gives explanations when needed and moves at a nice speed. I do wish the author (John Doran) went into more detail as to why he chose to do certain things a certain way instead of a perform these steps exactly like this approach. Following along with the book using Unity you do get to make the games from the book but not a whole lot of explaining as to why we need to do things a certain way. So creating your own game from scratch will prove to be problematic for those without prior experience. The chapter on UI is now a little dated due to the new UI system in Unity 4.6 but thankfully there is another chapter later in the book going over the new UI system in Unity 4.6. Kudos for the Author on providing information on both UI systems for the readers. The chapters on creating a First Person Shooter are my favorite, there was a lot of thought and effort into how best to explain things to a beginner. Creating Exterior Environments actually covers a lot of Unity tools but its doesn’t seem crammed or rushed. It walks you thru creating outside environments and the tools you will use in such a way that you forget you are learning and just get immersed into the FPS environment you are creating. Also props to the author for having us to a nighttime scene, I always see examples and tutorials on daytime environments but none on nighttime environments. In my opinion its much harder to get the realism and general creepiness of a nighttime forrest environment then some sunny blue sky perfect green grass environments most books teach you with. Now would be a good time to note that the author takes time to explain the different roles when it comes to game development so the reader can see the different areas involved. One chapter we are creating the outside environment as an environmental artist and the next we are creating interiors as a level designer. its great to see the different roles and how everything comes together. The interior environments chapter goes over modular level design with tiles which is what many of the larger AAA games use. Its a topic that is hard to find good information on but the author nails it in chapter 5. The rest of the FPS chapters go over AI, state machines, saving and some other great topics. Like I said previously, the chapters on creating a First Person Shooter were my favorite. There are so many good nuggets of info in those chapters I found myself reading the over and over trying to catch everything.Overall Unity Game Development Blueprints is a good book that covers a lot of topics and concepts of game development. This book shows the correct way of game development that can be used in real world AAA games. It doesn’t do what most books do and show you just a watered down way leaving you with many questions and lack of understanding of how the AAA titles and developers work. John Doran has over 10 years in the industry and it really shows in this book. I would easily recommend this to anyone wanting to learn the proper way to develop games just like the big guys
Amazon Verified review Amazon
W Boudville Dec 21, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The screen shots in the book of the Unity development platform show a comprehensive array of options. All of which you can use to write a compelling interactive game. The book walks through extended examples that give source code snippets. So while you are interested in how the end user would play a game, the book describes another type of interplay. Between using the Unity GUI and the resultant source code as well as (of course!) the images seen by a player.The presumption is of little prior game writing. The easiest way to see this might be in chapter 3, where the switch statement is introduced and explained. This is pretty basic stuff to anyone who has programmed in any language! What is happening is that the book is pitched towards a reader who has a good [presumably] game idea to storyboard, but perhaps has little actual programming nous.The chapters end with a section called Challenges. These have suggestions about how to extend the example in each chapter. If this is indeed new programming to you, good advice might be to take up those suggestions. You can learn more by doing. Deepens your knowledge of using Unity.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela