Book Image

Unity 5.x By Example

By : Alan Thorn
Book Image

Unity 5.x By Example

By: Alan Thorn

Overview of this book

Unity is an exciting and popular engine in the game industry. Throughout this book, you’ll learn how to use Unity by making four fun game projects, from shooters and platformers to exploration and adventure games. Unity 5 By Example is an easy-to-follow guide for quickly learning how to use Unity in practical context, step by step, by making real-world game projects. Even if you have no previous experience of Unity, this book will help you understand the toolset in depth. You'll learn how to create a time-critical collection game, a twin-stick space shooter, a platformer, and an action-fest game with intelligent enemies. In clear and accessible prose, this book will present you with step-by-step tutorials for making four interesting games in Unity 5 and explain all the fundamental concepts along the way. Starting from the ground up and moving toward an intermediate level, this book will help you establish a strong foundation in making games with Unity 5.
Table of Contents (15 chapters)
Unity 5.x By Example
About the Author
About the Reviewer


Video games are a cultural phenomenon that has captivated, entertained, and moved billions of people worldwide over the past fifty years. As an industry and movement, video games are an exciting place to be, both for the developer and the artist. In these roles your vision, ideas, and work can influence wide audiences, shaping and changing generation after generation in an unprecedented way. In more recent times, there's been a general movement towards democratizing game development, making the development process simpler, smoother and more accessible to a wider audience, including developers perhaps working from home on a very limited budget. Instrumental in this movement is the Unity engine, which forms the main subject of this book. The Unity engine is a computer program that works with your existing asset pipeline (such as 3D modeling software) and is intended for compiling video games that work seamlessly across multiple platforms and devices, including Windows, Mac, Linux, Android, iOS and Windows Phone. Using Unity, developers import ready-made assets (such as music, textures, and 3D models), and assemble them into a coherent whole, forming a game world that works by a unified logic. Unity is an amazing program. The latest version is free for most people to download and use, and it works well with many other programs, including free software such as GIMP and Blender. This book focuses on the Unity engine and how it can be used in a practical context for making playable and fun games. No prior knowledge of Unity is expected, although some knowledge of programming and scripting (such as JavaScript, ActionScript, C, C++, Java, or C#) would be beneficial. Let's now take a look at what this book covers, on a chapter-by-chapter basis.

What this book covers

This book explores how to use the Unity engine in a hands-on, practical way by looking at concrete examples that result in real-world playable games. Specifically, it focuses on the implementation of four distinct projects divided across eight chapters, two chapters per project. Let's take a look at what these projects are:

Chapter 1, The Coin Collection Game – Part 1, begins our journey into Unity by creating a first-person collection game. This is a great starting point if you're totally new to Unity and are ready to create your first game.

Chapter 2, Project A – the Collection Game Continued, continues from the previous chapter and completes the first project. It assumes that you have completed the first chapter and brings a closure to our project, leading neatly to the next chapter.

Chapter 3, Project B – the Space Shooter, marks the beginning of our second project, focusing on the creation of a space shooter game. Here, we'll create a project in which the player must shoot the oncoming enemies.

Chapter 4, Continuing the Space Shooter, completes the space shooter project, taking the project from its state in the previous chapter and adding final touches to it.

Chapter 5, Project C – a 2D Adventure, enters the world of 2D and UI functionality. Here, we'll explore Unity's wide range of 2D features in making a side-view platformer game that relies on 2D physics.

Chapter 6, Continuing the 2D Adventure, completes the 2D adventure game project that was started in the previous chapter, adding the final touches and linking it together with the overarching game logic. This is a great place to see how multiple parts and facets of a game come together to form a whole.

Chapter 7, Project D – Intelligent Enemies, focuses on artificial intelligence and creating enemies that can patrol, chase, and attack the player's character at relevant times, while cleverly navigating their way around the level.

Chapter 8, Continuing with Intelligent Enemies, brings closure to the AI project started in the previous chapter, as well as to the book content as a whole. Here, we'll see how to use finite-state machines to achieve powerful intelligence functionality that'll help us in a variety of scenarios.

What you need for this book

This book contains almost everything you need to follow along. Each chapter considers practical, real-world projects for learning Unity, and includes companion files which can be downloaded and used. The only thing you need, apart from this book and your concentration, is a copy of the latest version of Unity. At the time of writing, this is Unity 5.3.1. This software is available for free as a personal edition, and can be downloaded from the Unity website, here: In addition to Unity, if you want to create props, character models, and other 3D assets, you'll also need 3D modeling and animation software, such as 3DS Max, Maya, or Blender; you'll also need image editing software, such as Photoshop or GIMP. Blender can be downloaded and used for free here: And GIMP can be downloaded and used for free here:

Who this book is for

This book is ideally suited to readers with no prior experience of Unity or game development and who are considering game development as a hobby or a career. You will typically have some basic knowledge of programming or scripting, perhaps in contexts outside game development, in languages such as C, C++, C#, Java, JavaScript, ActionScript, Python, or another object-oriented language. In addition, you should have at least some basic idea of core game development concepts; for example, I'll assume you know what a 3D model is, what a texture is, what an audio file is, and what an executable file is. I take these concepts to be foundational. They will be touched upon on this book briefly, but not elaborated on or explained in depth. Here, we'll focus on Unity as software, as a tool for building real-world games. Each chapter reads as an in-depth tutorial, assembling a functioning product that you can expand upon and play.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The environment created thus far for the coin collection game has been assembled using only the mesh assets included with the native Prototyping package."

A block of code is set as follows:

using UnityEngine;
using System.Collections;

public class Coin : MonoBehaviour

  // Use this for initialization
  void Start () {}
  // Update is called once per frame
  void Update () {}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

using UnityEngine;
using System.Collections;

public class Coin : MonoBehaviour
  // Use this for initialization
  void Start () {
    Debug.Log ("Object Created");
  // Update is called once per frame
  void Update () {


New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You'll need to create New Project."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.