Unity 5.x By Example
By Alan Thorn
()
About this ebook
About This Book
- The most updated resource on Unity 5.x with comprehensive discussion on all the new features of Unity 5.x
- Understand the core concepts surrounding Unity5 game development with this power-packed hands-on guide
- Brush up your existing game development skills and create games that have a brilliant gameplay using the excellent examples from this book
Who This Book Is For
The ideal target audience for this book would be game developers. They need not have previous experience with Unity since this book will cover all the basics about game development with unity. This would also be a very good resource for Unity developers who want to brush up their basic Unity skills and also get up and running with creating interesting games with Unity 5.x.
What You Will Learn
- Understand core Unity concepts, such as game objects, components, and scenes
- Learn level design techniques for building immersive and interesting worlds
- Learn to make functional games with C# scripting
- Use the toolset creatively to build games of different themes and styles
- Learn to handle player controls and input functionality
- Dive into the process of working with terrains and world-creation tools
- Import custom content into Unity from third-party tools, such as Maya and Blender
- Get to grips with making both 2D and 3D games
In Detail
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.
Style and approach
This book would be a very unique resource for any game developer who wants to get up and running with Unity. The unique example based approach will take you through the most basic games towards the more complex ones and will gradually build your skill level.
Read more from Alan Thorn
Mastering Unity Scripting Rating: 0 out of 5 stars0 ratingsUnity Animation Essentials Rating: 0 out of 5 stars0 ratingsMastering Unity 5.x Rating: 0 out of 5 stars0 ratingsMoving from Unity to Godot: An In-Depth Handbook to Godot for Unity Users Rating: 0 out of 5 stars0 ratings
Related to Unity 5.x By Example
Related ebooks
Getting Started with Unity 5 Rating: 5 out of 5 stars5/5Unity Game Development Blueprints Rating: 0 out of 5 stars0 ratingsBuilding Levels in Unity Rating: 0 out of 5 stars0 ratingsBuilding a Game with Unity and Blender Rating: 0 out of 5 stars0 ratings3D Game Design with Unreal Engine 4 and Blender Rating: 0 out of 5 stars0 ratingsProcedural Content Generation for Unity Game Development Rating: 0 out of 5 stars0 ratingsBlueprints Visual Scripting for Unreal Engine Rating: 0 out of 5 stars0 ratingsUnity from Proficiency to Mastery (Artificial Intelligence): Unity from Proficiency to Mastery, #1 Rating: 0 out of 5 stars0 ratingsA Beginner's Guide to 2D Shooter Games: Beginners' Guides, #2 Rating: 0 out of 5 stars0 ratingsUnreal Engine Game Development Cookbook Rating: 0 out of 5 stars0 ratingsUnreal Engine 4 Game Development Essentials Rating: 4 out of 5 stars4/5A Quick Guide to Card Games with Unity: Quick Guides, #5 Rating: 0 out of 5 stars0 ratingsLearning C# by Developing Games with Unity 5.x - Second Edition Rating: 5 out of 5 stars5/5Building an FPS Game with Unity Rating: 0 out of 5 stars0 ratingsUnity from Zero to Proficiency (Proficient): Unity from Zero to Proficiency, #5 Rating: 5 out of 5 stars5/5Unity AI Programming Essentials Rating: 0 out of 5 stars0 ratingsUnity from Zero to Proficiency (Intermediate): Unity from Zero to Proficiency, #3 Rating: 2 out of 5 stars2/5Mastering Unreal Engine 4.X Rating: 0 out of 5 stars0 ratingsBuilding an RPG with Unreal Rating: 0 out of 5 stars0 ratingsUnity from Zero to Proficiency (Advanced): Unity from Zero to Proficiency, #4 Rating: 0 out of 5 stars0 ratingsUnity Multiplayer Games Rating: 5 out of 5 stars5/5Mobile Game Design Essentials Rating: 0 out of 5 stars0 ratingsA Quick Guide to Artificial Intelligence with Unity: Quick Guides, #4 Rating: 0 out of 5 stars0 ratingsUnreal Engine Physics Essentials Rating: 1 out of 5 stars1/5A Beginner's Guide to 2D Platform Games with Unity: Beginners' Guides, #1 Rating: 0 out of 5 stars0 ratingsUnity from Proficiency to Mastery (C# Programming): Unity 5 from Proficiency to Mastery, #2 Rating: 0 out of 5 stars0 ratingsA Quick Guide to 2d Infinite Runners with Unity: Quick Guides, #3 Rating: 0 out of 5 stars0 ratingsBlueprints Visual Scripting for Unreal Engine - Second Edition: The faster way to build games using UE4 Blueprints, 2nd Edition Rating: 0 out of 5 stars0 ratingsUnity from Zero to Proficiency (Beginner): Unity from Zero to Proficiency, #2 Rating: 5 out of 5 stars5/5Unreal Engine 4 AI Programming Essentials Rating: 0 out of 5 stars0 ratings
Programming For You
HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5
Reviews for Unity 5.x By Example
0 ratings0 reviews
Book preview
Unity 5.x By Example - Alan Thorn
Table of Contents
Unity 5.x By Example
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. The Coin Collection Game – Part 1
Game design
Getting started – Unity and projects
Projects and project folders
Importing assets
Starting a level
Transformations and navigation
Scene building
Lighting and sky
Play testing and the Game tab
Adding a water plane
Adding a coin to collect
Summary
2. Project A – the Collection Game Continued
Creating a coin material
C# scripting in Unity
Counting coins
Code Sample 2.3
Collecting coins
Code Sample 2.5
Coins and prefabs
Timers and countdowns
Code Sample 2.6
Celebrations and fireworks!
Code Sample 2.7
Play testing
Building
Summary
3. Project B – the Space Shooter
Looking ahead – the completed project
Getting started with a space shooter
Creating a player object
Player input
Code Sample 3.1
Configuring the game camera
Bounds locking
Code Sample 3.2
Health
Code Sample 3.3
Death and particles
Code Sample 3.4
Enemies
Code Sample 3.6
Code Sample 3.7
Code Sample 3.8
Enemy spawning
Code Sample 3.9
Summary
4. Continuing the Space Shooter
Guns and gun turrets
Ammo prefabs
Code Sample 4-1
Ammo spawning
Code Sample 4.2
Code Sample 4.3
User controls
Scores and scoring – UI and text objects
Working with scores – scripting with text
Code Sample 4.4
Polishing
Testing and diagnosis
Building
Summary
5. Project C – a 2D Adventure
A 2D Adventure – getting started
Importing assets
Creating an environment – getting started
Environment Physics
Creating a player
Scripting the player movement
Code Sample 5.1
Optimization
Summary
6. Continuing the 2D Adventure
Moving platforms
Code Sample 6.1
Creating other scenes – levels 2 and 3
Kill zones
Code Sample 6.2
The UI health bar
Code Sample 6.3
Ammo and hazards
Gun turrets and ammo
NPCs and quests
Code Sample 6.8
Summary
7. Project D – Intelligent Enemies
An overview of the project
Getting started
Terrain construction
Navigation and navigation meshes
Building an NPC
Code Sample 7.1
Creating patrolling NPCs
Summary
8. Continuing with Intelligent Enemies
Enemy AI – range of sight
Code Sample 8.1
An overview of Finite State Machines
Code Sample 8.2
The Patrol state
Code Sample 8.3
The Chase state
Code Sample 8.4
The Attack state
Code Sample 8.5
Summary
Index
Unity 5.x By Example
Unity 5.x By Example
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2016
Production reference: 1180316
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-838-0
www.packtpub.com
Credits
Author
Alan Thorn
Reviewer
Francesco Sapio
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Nadeem Bagban
Content Development Editor
Preeti Singh
Technical Editors
Suwarna Patil
Bharat Patil
Copy Editor
Tasneem Fatehi
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
About the Author
Alan Thorn is an award-winning author, mathematician, and independent video game developer based in London, UK. He is the founder of the game development studio, Wax Lyrical Games, and the creator of the critically acclaimed PC adventure game, Baron Wittard: Nemesis of Ragnarok. Alan works freelance for some of the world's largest entertainment corporations. He has lectured on game development at some of the most prestigious institutions in Europe and written nine books on games programming, including the highly popular Teach Yourself Games Programming, Game Engine Design and Implementation, and UDK Game Development. Some of Alan's other interests include computing, mathematics, graphics, and philosophy. More information about his company, Wax Lyrical Games, can be found at http://www.waxlyricalgames.com/.
About the Reviewer
Francesco Sapio obtained his computer science and control engineering degree from Sapienza University of Rome, Italy, with a couple of semesters in advance, scoring summa cum laude. Now he is studying a master's of science in artificial intelligence and robotics.
He is a Unity3D expert and skilled game designer, as well as an experienced user of the major graphics programs.
Recently, he authored the book Unity UI Cookbook (Packt Publishing), which teaches readers how to develop exciting and practical user interfaces for games within Unity. Furthermore, he has also been a reviewer for the book Unity Game Development Scripting (Packt Publishing).
Francesco is also a musician and composer, especially for soundtracks of short films and video games. For several years, he worked as an actor and dancer. He was a guest of honor at the Brancaccio theatre in Rome.
In addition, he is a very active person, having volunteered as a children's entertainer at the Associazione Culturale Torraccia in Rome. Also, he gives private lessons in mathematics and music to high school and university students.
Francesco loves math, philosophy, logic, and puzzle solving, but most of all, creating video games—thanks to his passion for game designing and programming.
You can find him at https://linkedin.com/pub/francesco-sapio/b8/5b/365.
I'm deeply thankful to my parents for their infinite patience, enthusiasm, and support throughout my life. Moreover, I'm thankful to the rest of my family, in particular, my grandparents as they always encouraged me to do better in my life with the Latin expressions Ad Maiora and Per aspera ad astra.
Finally, a huge thanks to all the special people around me whom I love, in particular, my girlfriend; I'm grateful for all your help with everything.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
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: https://unity3d.com/. 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: http://www.blender.org/. And GIMP can be downloaded and used for free here: https://www.gimp.org/.
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.
Conventions
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.
Note
Warnings or important notes appear in a box like this.
Tip
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 <feedback@packtpub.com>, 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 www.packtpub.com/authors.
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 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
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 https://www.packtpub.com/sites/default/files/downloads/Unity5xByExample_ColorImages.pdf.
Errata
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 http://www.packtpub.com/submit-errata, 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 https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
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 <copyright@packtpub.com> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>, and we will do our best to address the problem.
Chapter 1. The Coin Collection Game – Part 1
This chapter starts the first project on our list, which will be a fun collection game. Remember, it doesn't matter if you've never used Unity before. We'll go through everything necessary step by step. By the end of the next chapter, you'll have pieced together a simple, but complete and functional, game. This is an important thing to achieve because you'll get familiar with a start-to-end game development workflow. This chapter will demonstrate the following topics:
Game design
Projects and folders
Asset importing and configuration
Level design
Game objects
Hierarchies
Game design
Let's make a coin collection game. Here, the player should control a character in the first-person mode, and he must wander the level, collecting all coins before a time limit runs out. If the timer runs out, the game is lost. On the other hand, if all coins are collected before the timer expires, the game is won. The first-person controls will use the default WASD keyboard setup, where W moves forward, A and S move left and right, and D walks backward. Head movement is controlled using the mouse, and coins are collected by simply walking into them. See Figure 1.1, featuring the coin collection game in action in the Unity Editor. The great benefit in making this game is that it demonstrates all the core Unity features together, and we don't need to rely on any external software to make assets, such as textures, meshes, and materials.