43,19 €
A project-based guide to help you create amazing games with Unity 5.x
This book is best suited for C# developers who have some basic knowledge of the Unity Game development platform. If you are looking to create exciting and interactive games with Unity and get a practical understanding of how to leverage key Unity features and then optimize the Unity rendering engine, then this book is your one-stop solution.
This book will help you to create exciting and interactive games from scratch with the Unity game development platform. We will build 7-8 action-packed games of different difficulty levels, and we'll show you how to leverage the intuitive workflow tools and state of the art Unity rendering engine to build and deploy mobile desktop as well as console games.
Through this book, you'll develop a complete skillset with the Unity toolset. Using the powerful C# language, we'll create game-specific characters and game environments. Each project will focus on key Unity features as well as game strategy development. This book is the ideal guide to help your transition from an application developer to a full-fledged Unity game developer
A step by step approach to develop a strong Unity skillset by creating a few action-packed games from scratch.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 348
Veröffentlichungsjahr: 2016
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: May 2016
Production reference: 1190516
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-311-8
www.packtpub.com
Author
John P. Doran
Reviewer
Sebastian T. Koenig, Ph.D.
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Larissa Pinto
Content Development Editor
Prashanth G Rao
Technical Editor
Mohit Hassija
Copy Editor
Dipti Mankame
Project Coordinator
Bijal Patel
Proofreader
Safis Editing
Indexer
Rekha Nair
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
John P. Doran is a technical game designer who has been creating games for over 10 years. He has worked on an assortment of games in teams consisting of just himself to over 70 people in student, mod, and professional projects in different roles, from game designer to lead UI programmer. He previously worked at LucasArts on Star Wars: 1313 as a game designer. He later graduated from DigiPen Institute of Technology in Redmond, WA, with a Bachelor of Science in game design.
John is currently a part of DigiPen's Research and Development branch in Singapore in addition to DigiPen Game Studios. He is also the lead instructor of the DigiPen-Ubisoft Campus Game Programming Program, instructing graduate-level students in an intensive, advanced-level game programming curriculum. In addition to that, he also tutors and assists students in various subjects and gives lectures on C#, C++, Unreal, Unity, game design, and more.
He is the author of Unreal Engine Game Development Cookbook, Building an FPS Game in Unity, Unity Game Development Blueprints, Getting Started with UDK, UDK Game Development, Mastering UDK Game Development, and cowrote UDK iOS Game Development Beginner's Guide, all available from Packt Publishing. More information about him can be found at http://johnpdoran.com.
A big thanks goes to my brother Chris and my wife Hien for being supportive and patient with me as I spent my free time and weekends away from them to finish the book.
On that same note, I also want to thank Samir Abou Samra and Elie Hosry for their support and encouragement while working on this book, as well as the rest of the DigiPen Singapore staff.
Having the opportunity to create an updated version of one of my favorite books was such a pleasure. With that in mind, I want to thank Nadeem Bagban for approaching me, Joel Ulahanna and Prashanth G for keeping me on track, and everyone else at Packt for being great as always.
Last but not least, I'd like to thank my family as well as my parents Joseph and Sandra Doran, who took me seriously when I told them I wanted to make games for a living.
Sebastian T. Koenig, Ph.D., received his doctorate in human interface technology from the University of Canterbury, New Zealand, developing a framework for individualized virtual reality cognitive rehabilitation. He obtained his diploma in psychology from the University of Regensburg, Germany, in the areas of clinical neuropsychology and virtual reality rehabilitation.
Dr. Koenig is the founder and CEO of Katana Simulations, where he oversees the design, development, and evaluation of cognitive assessment and training simulations. His professional experience spans over 10 years of clinical work in cognitive rehabilitation and virtual reality research, development, and user testing. Dr. Koenig has extensive experience as a speaker at international conferences and as a reviewer of scientific publications in the areas of rehabilitation, cognitive psychology, neuropsychology, software engineering, game development, game user research, and virtual reality.
Dr. Koenig has developed numerous software applications for cognitive assessment and training. For his work on the Virtual Memory Task, he was awarded the prestigious Laval Virtual Award in 2011 in the Medicine and Health category. Other applications of his include the virtual reality executive function assessment in collaboration with the Kessler Foundation, NJ, USA, and the patent-pending Microsoft Kinect-based motor and cognitive training JewelMine/Mystic Isle software at the USC Institute for Creative Technologies, CA, USA.
Dr. Koenig maintains the website http://www.virtualgamelab.com about his research and software development projects. His website also contains a comprehensive list of tutorials for the Unity game engine.
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 <[email protected]> for more details.
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.
Unity, available in free and pro versions, is one of the most popular third-party game engines available. It is a cross-platform game engine, making it easy to write your game once and then port it to PC, consoles, and even the Web, which makes it a great choice for both indie and AAA developers.
Unity 5.x Game Development Blueprints will takes you on an exploration of using Unity to the fullest extent, working on 3D and 2D titles, exploring how to create GUIs, and publishing the game for the world to see. Getting the opportunity to make a second edition of this book, I've updated each of the projects from the previous book to the latest version of Unity, making use of the latest features while also adding in three entirely new game projects for this book.
Using this book, you will be able to create a 2D twin-stick shooter, a clicker title, an endless runner, a shooting gallery, a side-scrolling platformer with an in-game level editor, a first-person survival horror shooter game, and a GUI menu system to use in all your future titles. In addition, you will learn how to publish your game with an installer to make your title look really polished and stand out from the crowd.
Each chapter either pushes your skills in Unity into new areas or pushes them to the very limits of what they can be used for.
Chapter 1, 2D Twin-stick Shooter, shows you how to create a 2D multidirectional shooter game. In this game, the player controls a ship that can move around the screen using the keyboard and shoot projectiles in the direction the mouse is pointing at. Enemies and obstacles will spawn towards the player, and the player will avoid/shoot them. This chapter also serves as a refresher for a lot of the concepts of working in Unity and gives an overview of Unity's native 2D tools.
Chapter 2, Creating GUIs, expands on our twin-stick shooter game, adding additional UI elements, including a main menu as well as a pause menu and options menu, and it gives us the ability to restart our project.
Chapter 3, GUIs Part 2 – Clicker Game, expands on our GUI knowledge with the implementation of a clicker game. Over the course of the chapter, you learn how to create and animate buttons, how to work with accessors (get/set functions), and how to build a simple shop for your projects dealing with UI assets such as Scrollviews and Masks.
Chapter 4, Mobile Endless Game – Procedural Content, has us building a game similar in gameplay to the popular mobile title Flappy Bird while learning how to create endless levels by creating content at runtime. In addition, we go over topics such as creating a repeating background, using sorting layers.
Chapter 5, Shooting Gallery – Working with Animations and Tweens, has you build a simple shooting gallery game in which players can shoot at targets before the time is up. In the meantime, you learn the various ways of animating things such as using Unity's built in animations or a tweening library such as iTween. Finally, we will use PlayerPrefs to set a new high score.
Chapter 6, Side-scrolling Platformer, shows you how to create a side-scrolling platformer. You learn the similarities between working in 2D and 3D and the differences, in particular, when it comes to physics.
Chapter 7, First Person Shooter Part 1 – Creating Exterior Environments, discusses the role of an environment artist who has been tasked to create an outdoor environment while learning about mesh placement. In addition, we will also learn some beginner-level design.
Chapter 8, First Person Shooter Part 2 – Creating Interior Environments, discusses the role of a level designer who has been tasked to create an interior environment using assets already provided to them by the environment artist.
Chapter 9, First Person Shooter Part 3 – Implementing Gameplay and AI, shows how we are going to be adding interactivity in the form of adding in enemies, shooting behaviors, and the gameplay to make our game truly shine. In addition, we'll also learn how to use an Xbox 360 controller to send input to our game.
Chapter 10, Building an In-Game Level Editor, talks about how we can add in functionality to our previously created side-scrolling platformer game in the form of an in-game level editor, which can be used for future projects. In addition, we'll also look at the Immediate Mode GUI System (IMGUI) to see how we can create GUI elements directly through code.
Chapter 11, Finishing Touches, talks about exporting our game from Unity and then creating an installer so that we can give it to all of our friends, family, and prospective customers!
Throughout this book, we will work within the Unity 3D game engine, which you can download from http://unity3d.com/unity/download/. The projects have been created using version 5.3.4f1 but should work with minimal changes for future versions.
For the sake of simplicity, we will assume that you are working on a Windows-powered computer. Although Unity allows you to code in either C#, Boo, or UnityScript, for this book, we will be using C#.
This book is best suited for C# developers who have some basic knowledge of the Unity game development platform. If you are looking to create exciting and interactive games with Unity and get a practical understanding of how to leverage key Unity features, then this book is your one-stop solution.
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 <[email protected]>, 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.
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.
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:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Unity-5.x-Game-Development-Blueprints. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
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/Unity5xGameDevelopmentBlueprints_ColorImages.pdf.
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 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 <[email protected]> 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 <[email protected]>, and we will do our best to address the problem.
The shoot 'em up genre of games is one of the earliest kinds of game. In a shoot 'em up, the player character is a single entity fighting a large number of enemies. They are typically played using a top-down or side-scrolling perspective, which is perfect for 2D games. Shoot 'em up games also exist within many other 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 gone through a resurgence in recent years with games such as Bizarre Creations' Geometry Wars: Retro Evolved, which is more popularly known as a twin-stick shooter.
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 is pointing. Enemies and obstacles will spawn toward the player, and the player will avoid/shoot them. This chapter will also serve as a refresher for a lot of the concepts of working in Unity and give an overview of the native 2D tools in Unity.
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:
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:
From here, you'll want to select the Choose your Unity + Download button and from the following menu select the edition of Unity you'd like to use and then download the Launcher.
Everything that we will be covering will work in the Personal Edition, unless I mention specifically otherwise. At the time of writing, the latest version is 5.3.4, but the project should work with minimal changes in future versions.
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, download the Chapter1.zip package, and unzip it. Inside the Chapter1 folder, there are a number of things, including an Art Assets folder, which will have the art, sound, and font files you'll need for the project as well as the Twinstick Shooter folder which contains the finished project for you to take a look at.
At this point, I assume that you have Unity freshly installed and have started it up.
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 in the long run and only takes a few seconds, which we'll be doing now. Perform the following steps:
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.
Now that we have our project set up, let's get started with creating our player:
If you do not want to drag and drop the files, you can also right-click within the Sprites folder in Project Browser (bottom-left corner) and select Import New Asset to select a file from the Chapter 1/Art Assets folder to bring it in.
The art assets used for this tutorial were provided by Kenney. To see more of their work, please check out www.kenney.nl.
Now, with the player in the world, let's add in a background. However, instead of creating a huge image or copying and pasting a similar image over and over, we will learn how we can use a material with a repeating texture.
From the Project tab, bring the background sprite into the project and then select it and change the Texture Type in the Inspector tab to Texture, and click on Apply.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 Position to (0, 0, 1) and the scale to (100, 100, 1).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 pixilation, 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.
As we won't be using it (and we're a 2D game), remove the BoxCollider component by right clicking on it from the Inspector tab and then selecting Remove Component.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 BackgroundMaterial. We can create a new folder called Materials to store this, but since this project will only use one it is OK to stay in Sprites. 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: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.
You can also add a material to an object by dragging and dropping the material onto the object in the Scene tab.
Well, at this point we have a color, but we don't have the stars from our image repeating.. This is due to how the Wrap Mode property is set. In Unity 5.2, the default mode is Clamp, which means that the edges of the image will be extended out rather than repeat. Let's fix that now.
From the Project tab, select the background texture object. Once selected, go to the Inspector tab, change the Wrap Mode to Repeat, and then click on Apply. Finally, select the Cube object from Hierarchy and then in the Inspector tab at the top, rename the object to Background.Now, when we play the game, you'll see that we now have a complete background that tiles properly.
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 modeled 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.