learn unity 3d programming with unityscript - springer978-1-4302-6587-0/1.pdf · learn unity 3d...
TRANSCRIPT
Learn Unity 3D Programming with
UnityScriptUnity’s JavaScript for Beginners
Janine Suvak
Learn Unity 3D Programming with UnityScript: Unity’s JavaScript for Beginners
Copyright © 2014 by Janine Suvak
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-6586-3
ISBN-13 (electronic): 978-1-4302-6587-0
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Publisher: Heinz Weinheimer Lead Editor: Louise Corrigan Technical Reviewer: Graham McAllister Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Jim DeWolf,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss
Coordinating Editor: Christine Ricketts Copy Editor: James Fraleigh Compositor: SPi Global Indexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.
For Rob, Paige, Justin, and Taylor: for enriching my life and making every day worthwhile.
v
Contents at a Glance
About the Author �������������������������������������������������������������������������������������������������������������� xvii
About the Technical Reviewer ������������������������������������������������������������������������������������������� xix
Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi
Introduction ��������������������������������������������������������������������������������������������������������������������� xxiii
Chapter 1: Getting Started with Unity ■ ��������������������������������������������������������������������������������1
Chapter 2: Game Programming 101 ■ ��������������������������������������������������������������������������������27
Chapter 3: Making a Simple Scene ■ ����������������������������������������������������������������������������������61
Chapter 4: Using Scripts and the GameObject ■ �����������������������������������������������������������������85
Chapter 5: Moving the GameObject ■ �������������������������������������������������������������������������������109
Chapter 6: Starting with Coding Physics ■ �����������������������������������������������������������������������157
Chapter 7: Using Advanced Physics Concepts ■ ��������������������������������������������������������������187
Chapter 8: Particle Emitters and Special Effects ■ �����������������������������������������������������������221
Chapter 9: Game Design and Logic—The Blueprint ■ ������������������������������������������������������249
Chapter 10: Putting the Pieces Together and Building Your Game ■ ��������������������������������269
Chapter 11: Enhancing The User Experience: GUI and Sound ■ ����������������������������������������303
vi Contents at a Glance
Chapter 12: Optimizing Your Game ■ ��������������������������������������������������������������������������������337
Chapter 13: Where to Go from Here ■ �������������������������������������������������������������������������������363
Index ���������������������������������������������������������������������������������������������������������������������������������387
vii
Contents
About the Author �������������������������������������������������������������������������������������������������������������� xvii
About the Technical Reviewer ������������������������������������������������������������������������������������������� xix
Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi
Introduction ��������������������������������������������������������������������������������������������������������������������� xxiii
Chapter 1: Getting Started with Unity ■ ��������������������������������������������������������������������������������1
What Is UnityScript? ����������������������������������������������������������������������������������������������������������������������2
Prerequisites ���������������������������������������������������������������������������������������������������������������������������������2
Meet Unity �������������������������������������������������������������������������������������������������������������������������������������3
Setting Up the Development Environment ������������������������������������������������������������������������������������7
Getting Started with AngryBots ���������������������������������������������������������������������������������������������������16
Game View (+2) ����������������������������������������������������������������������������������������������������������������������������������������������� 17
Hierarchy View (+4) ����������������������������������������������������������������������������������������������������������������������������������������� 20
Scene View (+1) ����������������������������������������������������������������������������������������������������������������������������������������������� 22
Inspector (+3) �������������������������������������������������������������������������������������������������������������������������������������������������� 24
Project Panel (+5) �������������������������������������������������������������������������������������������������������������������������������������������� 24
Console (SHIFT++C) ����������������������������������������������������������������������������������������������������������������������������������������� 25
Summary �������������������������������������������������������������������������������������������������������������������������������������25
viii Contents
Chapter 2: Game Programming 101 ■ ��������������������������������������������������������������������������������27
MonoDevelop �������������������������������������������������������������������������������������������������������������������������������32
Variables and Functions ��������������������������������������������������������������������������������������������������������������39
Conditionals ���������������������������������������������������������������������������������������������������������������������������������47
Boolean Logic and Conditionals �������������������������������������������������������������������������������������������������������������������������� 47
Loops �������������������������������������������������������������������������������������������������������������������������������������������51
The for Loop ��������������������������������������������������������������������������������������������������������������������������������������������������������� 51
The while Loop ���������������������������������������������������������������������������������������������������������������������������������������������������� 52
The do-while Loop ����������������������������������������������������������������������������������������������������������������������������������������������� 53
Coordinate Geometry and Vectors �����������������������������������������������������������������������������������������������54
Summary �������������������������������������������������������������������������������������������������������������������������������������60
Chapter 3: Making a Simple Scene ■ ����������������������������������������������������������������������������������61
New Project, New Scene �������������������������������������������������������������������������������������������������������������62
The Game Object �������������������������������������������������������������������������������������������������������������������������63
Components ��������������������������������������������������������������������������������������������������������������������������������64
The Main Camera ������������������������������������������������������������������������������������������������������������������������65
Transform ������������������������������������������������������������������������������������������������������������������������������������������������������������ 66
Camera ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 67
GUILayer �������������������������������������������������������������������������������������������������������������������������������������������������������������� 71
Flare Layer ����������������������������������������������������������������������������������������������������������������������������������������������������������� 71
Audio Listener ������������������������������������������������������������������������������������������������������������������������������������������������������ 71
Light ��������������������������������������������������������������������������������������������������������������������������������������������72
Common Light Properties ������������������������������������������������������������������������������������������������������������������������������������ 72
Other Properties �������������������������������������������������������������������������������������������������������������������������������������������������� 75
The Cube �������������������������������������������������������������������������������������������������������������������������������������76
Transform ������������������������������������������������������������������������������������������������������������������������������������������������������������ 77
Mesh Filter ����������������������������������������������������������������������������������������������������������������������������������������������������������� 77
Box Collider ��������������������������������������������������������������������������������������������������������������������������������������������������������� 77
ixContents
Mesh Renderer ���������������������������������������������������������������������������������������������������������������������������������������������������� 79
Material ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 79
Prefabs ����������������������������������������������������������������������������������������������������������������������������������������79
Summary �������������������������������������������������������������������������������������������������������������������������������������84
Chapter 4: Using Scripts and the GameObject ■ �����������������������������������������������������������������85
Scripts as Behavior Components ������������������������������������������������������������������������������������������������86
Getting the Game Started ������������������������������������������������������������������������������������������������������������90
Awake ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 90
Start ��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 90
Update ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 91
Scripting in Start �������������������������������������������������������������������������������������������������������������������������������������������������� 92
Color �������������������������������������������������������������������������������������������������������������������������������������������������������������������� 93
Frame Rates �������������������������������������������������������������������������������������������������������������������������������������������������������� 95
Time�deltaTime ���������������������������������������������������������������������������������������������������������������������������������������������������� 96
Scripting in Update ���������������������������������������������������������������������������������������������������������������������������������������������� 96
User Input ������������������������������������������������������������������������������������������������������������������������������������������������������������ 97
Cleaning Up Your Code����������������������������������������������������������������������������������������������������������������������������������������� 98
Enabling and Disabling Components ����������������������������������������������������������������������������������������������������������������� 100
Destroying Game Objects ���������������������������������������������������������������������������������������������������������������������������������� 103
Instantiation ������������������������������������������������������������������������������������������������������������������������������104
Summary �����������������������������������������������������������������������������������������������������������������������������������107
Chapter 5: Moving the GameObject ■ �������������������������������������������������������������������������������109
Rotate ����������������������������������������������������������������������������������������������������������������������������������������109
Editing Properties While Playtesting ������������������������������������������������������������������������������������������������������������������ 111
Add User Interaction ������������������������������������������������������������������������������������������������������������������������������������������ 112
Translate ������������������������������������������������������������������������������������������������������������������������������������113
Transform�position ��������������������������������������������������������������������������������������������������������������������������������������������� 113
Smoothing It Out with Lerp �������������������������������������������������������������������������������������������������������114
Mecanim������������������������������������������������������������������������������������������������������������������������������������116
I’m a Doctor, Not a � � � ! �������������������������������������������������������������������������������������������������������������116
x Contents
Modeling, Rigging, and Skinning �����������������������������������������������������������������������������������������������117
The Asset Store �������������������������������������������������������������������������������������������������������������������������119
Import a Character Model ����������������������������������������������������������������������������������������������������������124
Create and Configure the Avatar �����������������������������������������������������������������������������������������������125
Import the Animation �����������������������������������������������������������������������������������������������������������������127
Create an Animator Controller ���������������������������������������������������������������������������������������������������128
Run Zombie Run ������������������������������������������������������������������������������������������������������������������������130
Run Like a Zombie ���������������������������������������������������������������������������������������������������������������������131
More Animations �����������������������������������������������������������������������������������������������������������������������132
Configure the Animations ����������������������������������������������������������������������������������������������������������136
Root Motion �������������������������������������������������������������������������������������������������������������������������������137
OnAnimatorMove �����������������������������������������������������������������������������������������������������������������������140
Transitions ���������������������������������������������������������������������������������������������������������������������������������142
Any State �����������������������������������������������������������������������������������������������������������������������������������147
User Input and the Input Manager ���������������������������������������������������������������������������������������������147
Blend Trees ��������������������������������������������������������������������������������������������������������������������������������149
Don’t Reinvent the Wheel ����������������������������������������������������������������������������������������������������������151
Summary �����������������������������������������������������������������������������������������������������������������������������������156
Chapter 6: Starting with Coding Physics ■ �����������������������������������������������������������������������157
Rigidbodies ��������������������������������������������������������������������������������������������������������������������������������157
Rigidbody Properties ����������������������������������������������������������������������������������������������������������������������������������������� 158
Controlling a Rigidbody with Forces ������������������������������������������������������������������������������������������������������������������ 159
Colliders ������������������������������������������������������������������������������������������������������������������������������������162
OnCollision ��������������������������������������������������������������������������������������������������������������������������������������������������������� 163
The Other GameObject ��������������������������������������������������������������������������������������������������������������������������������������� 165
Target Practice ��������������������������������������������������������������������������������������������������������������������������166
Physic Materials ������������������������������������������������������������������������������������������������������������������������170
Bounciness �������������������������������������������������������������������������������������������������������������������������������������������������������� 171
xiContents
LookAt ���������������������������������������������������������������������������������������������������������������������������������������172
Accessing Other Game Objects ������������������������������������������������������������������������������������������������������������������������� 173
Triggers �������������������������������������������������������������������������������������������������������������������������������������174
OnTrigger ����������������������������������������������������������������������������������������������������������������������������������������������������������� 174
Rapid Prototyping ����������������������������������������������������������������������������������������������������������������������177
Spawning and Spawn Points �����������������������������������������������������������������������������������������������������180
Changing the Character Model ��������������������������������������������������������������������������������������������������183
Summary �����������������������������������������������������������������������������������������������������������������������������������186
Chapter 7: Using Advanced Physics Concepts ■ ��������������������������������������������������������������187
Setting Up the Obstacle Course �������������������������������������������������������������������������������������������������187
Lowering Platform Obstacle ������������������������������������������������������������������������������������������������������188
Joints and the Pendulum Obstacle ��������������������������������������������������������������������������������������������192
Fixed Joint ��������������������������������������������������������������������������������������������������������������������������������������������������������� 192
Hinge Joint �������������������������������������������������������������������������������������������������������������������������������������������������������� 193
Build the Pendulum Chain ��������������������������������������������������������������������������������������������������������������������������������� 194
Character Joint �������������������������������������������������������������������������������������������������������������������������������������������������� 196
Spring Joint ������������������������������������������������������������������������������������������������������������������������������������������������������� 205
Configurable Joint ��������������������������������������������������������������������������������������������������������������������������������������������� 206
Raycasting ���������������������������������������������������������������������������������������������������������������������������������206
Tags ������������������������������������������������������������������������������������������������������������������������������������������������������������������� 211
Layers ���������������������������������������������������������������������������������������������������������������������������������������213
Character Controller ������������������������������������������������������������������������������������������������������������������214
Summary �����������������������������������������������������������������������������������������������������������������������������������219
Chapter 8: Particle Emitters and Special Effects ■ �����������������������������������������������������������221
The Shuriken Particle System Engine ���������������������������������������������������������������������������������������221
The Grand Entrance �������������������������������������������������������������������������������������������������������������������227
Puff of Dust �������������������������������������������������������������������������������������������������������������������������������228
Energy Burst ������������������������������������������������������������������������������������������������������������������������������229
Particle Effect ����������������������������������������������������������������������������������������������������������������������������231
Land With Force�������������������������������������������������������������������������������������������������������������������������232
xii Contents
Bomb Obstacle ��������������������������������������������������������������������������������������������������������������������������234
Blizzard Obstacle �����������������������������������������������������������������������������������������������������������������������235
Laser Obstacle ���������������������������������������������������������������������������������������������������������������������������237
Laser Emitter �����������������������������������������������������������������������������������������������������������������������������237
Laser Beam �������������������������������������������������������������������������������������������������������������������������������238
Tracer Fire Obstacle ������������������������������������������������������������������������������������������������������������������244
Projectile Prefab ������������������������������������������������������������������������������������������������������������������������245
Projectile Launcher �������������������������������������������������������������������������������������������������������������������245
Summary �����������������������������������������������������������������������������������������������������������������������������������247
Chapter 9: Game Design and Logic—The Blueprint ■ ������������������������������������������������������249
Game �����������������������������������������������������������������������������������������������������������������������������������������249
Games, Minigames, and Microgames ���������������������������������������������������������������������������������������250
Game Design Considerations ����������������������������������������������������������������������������������������������������251
Target Audience and ESRB Ratings ������������������������������������������������������������������������������������������������������������������� 251
Game Genres ����������������������������������������������������������������������������������������������������������������������������������������������������� 253
Themes �������������������������������������������������������������������������������������������������������������������������������������������������������������� 254
Fun and the Game Experience ��������������������������������������������������������������������������������������������������255
Maps ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 255
Pacing ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 257
Balance �������������������������������������������������������������������������������������������������������������������������������������������������������������� 258
Progression �������������������������������������������������������������������������������������������������������������������������������������������������������� 260
Punishments and Rewards �������������������������������������������������������������������������������������������������������������������������������� 262
The Game Design Document �����������������������������������������������������������������������������������������������������263
The Game Development Team ��������������������������������������������������������������������������������������������������������������������������� 264
The One-Sheet ��������������������������������������������������������������������������������������������������������������������������������������������������� 265
The Concept Overview ��������������������������������������������������������������������������������������������������������������������������������������� 266
The GDD ������������������������������������������������������������������������������������������������������������������������������������������������������������� 268
Summary �����������������������������������������������������������������������������������������������������������������������������������268
xiiiContents
Chapter 10: Putting the Pieces Together and Building Your Game ■ ��������������������������������269
Implementing the Design ����������������������������������������������������������������������������������������������������������270
Playtesting and Cheat Codes �����������������������������������������������������������������������������������������������������271
Land Mine Zone ������������������������������������������������������������������������������������������������������������������������������������������������� 273
Moving Platform Zone���������������������������������������������������������������������������������������������������������������������������������������� 277
Cannon Zone ������������������������������������������������������������������������������������������������������������������������������������������������������ 280
Pendulum Zone �������������������������������������������������������������������������������������������������������������������������������������������������� 282
Laser Zone ��������������������������������������������������������������������������������������������������������������������������������������������������������� 287
Blizzard Zone ����������������������������������������������������������������������������������������������������������������������������������������������������� 292
Enemy Zone ������������������������������������������������������������������������������������������������������������������������������������������������������� 294
Debugging ���������������������������������������������������������������������������������������������������������������������������������297
Compiler Errors �������������������������������������������������������������������������������������������������������������������������������������������������� 297
Warnings������������������������������������������������������������������������������������������������������������������������������������������������������������ 299
Runtime Errors ��������������������������������������������������������������������������������������������������������������������������������������������������� 300
Exception Handling �������������������������������������������������������������������������������������������������������������������������������������������� 301
Summary �����������������������������������������������������������������������������������������������������������������������������������302
Chapter 11: Enhancing The User Experience: GUI and Sound ■ ����������������������������������������303
Creating GUI Controls ����������������������������������������������������������������������������������������������������������������303
Display Text �������������������������������������������������������������������������������������������������������������������������������������������������������� 304
Text Input ����������������������������������������������������������������������������������������������������������������������������������������������������������� 308
More Controls ���������������������������������������������������������������������������������������������������������������������������������������������������� 310
Customizing Controls ����������������������������������������������������������������������������������������������������������������������������������������� 316
Audio �����������������������������������������������������������������������������������������������������������������������������������������318
Audio Listener ���������������������������������������������������������������������������������������������������������������������������������������������������� 319
Audio Source ����������������������������������������������������������������������������������������������������������������������������������������������������� 320
3D vs� 2D Sound Properties ������������������������������������������������������������������������������������������������������������������������������� 322
Audio Clip ���������������������������������������������������������������������������������������������������������������������������������������������������������� 323
Audio Settings ��������������������������������������������������������������������������������������������������������������������������������������������������� 323
Audio Testing ����������������������������������������������������������������������������������������������������������������������������������������������������� 324
xiv Contents
Adding Audio to the Obstacle Course ����������������������������������������������������������������������������������������325
Permission and Credits �������������������������������������������������������������������������������������������������������������������������������������� 325
Music ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 327
Sound Effects ���������������������������������������������������������������������������������������������������������������������������������������������������� 334
Summary �����������������������������������������������������������������������������������������������������������������������������������336
Chapter 12: Optimizing Your Game ■ ��������������������������������������������������������������������������������337
Target Performance �������������������������������������������������������������������������������������������������������������������337
Tracking Your Target ������������������������������������������������������������������������������������������������������������������338
When and Where to Optimize ����������������������������������������������������������������������������������������������������340
Design Phase �����������������������������������������������������������������������������������������������������������������������������340
Development Phase�������������������������������������������������������������������������������������������������������������������341
Creating a Build Log ������������������������������������������������������������������������������������������������������������������341
Scripts ���������������������������������������������������������������������������������������������������������������������������������������344
Static Typing and #pragma strict ����������������������������������������������������������������������������������������������������������������������� 344
Caching Component Lookups ���������������������������������������������������������������������������������������������������������������������������� 345
More Scripting Optimization Alternatives ����������������������������������������������������������������������������������345
Use Triggers ������������������������������������������������������������������������������������������������������������������������������������������������������� 345
Object Pooling ���������������������������������������������������������������������������������������������������������������������������������������������������� 346
Coroutines ��������������������������������������������������������������������������������������������������������������������������������������������������������� 347
Mecanim Optimization Tips �������������������������������������������������������������������������������������������������������347
Project Settings �������������������������������������������������������������������������������������������������������������������������348
Quality Settings Manager ���������������������������������������������������������������������������������������������������������������������������������� 348
Physics Manager ����������������������������������������������������������������������������������������������������������������������������������������������� 351
Time Manager ���������������������������������������������������������������������������������������������������������������������������������������������������� 352
Audio Manager �������������������������������������������������������������������������������������������������������������������������������������������������� 352
Player Settings Manager ����������������������������������������������������������������������������������������������������������������������������������� 353
Tags and Layers Manager ���������������������������������������������������������������������������������������������������������������������������������� 354
Project Settings Classes ������������������������������������������������������������������������������������������������������������������������������������ 355
xvContents
More Graphics Optimizations ����������������������������������������������������������������������������������������������������355
Cameras ������������������������������������������������������������������������������������������������������������������������������������356
Textures �������������������������������������������������������������������������������������������������������������������������������������356
Shaders �������������������������������������������������������������������������������������������������������������������������������������358
Publishing ����������������������������������������������������������������������������������������������������������������������������������358
Changing Scenes �����������������������������������������������������������������������������������������������������������������������359
Summary �����������������������������������������������������������������������������������������������������������������������������������361
Chapter 13: Where to Go from Here ■ �������������������������������������������������������������������������������363
Scripting and Other Technical Resources ����������������������������������������������������������������������������������363
Technical Forums ����������������������������������������������������������������������������������������������������������������������364
Other Developer Forums �����������������������������������������������������������������������������������������������������������366
Platform-Specific Developer Resources ������������������������������������������������������������������������������������367
Casual Groups, Organizations, and Gatherings �������������������������������������������������������������������������373
Game Jams �������������������������������������������������������������������������������������������������������������������������������376
Contests and Awards �����������������������������������������������������������������������������������������������������������������377
Contests ������������������������������������������������������������������������������������������������������������������������������������377
Awards ��������������������������������������������������������������������������������������������������������������������������������������379
Code Sources ����������������������������������������������������������������������������������������������������������������������������380
More Than a Hobby �������������������������������������������������������������������������������������������������������������������381
Portfolio ������������������������������������������������������������������������������������������������������������������������������������������������������������� 381
Web Site ������������������������������������������������������������������������������������������������������������������������������������������������������������ 382
Tailor Your Presentation ������������������������������������������������������������������������������������������������������������������������������������� 382
Get Honest Feedback ����������������������������������������������������������������������������������������������������������������������������������������� 382
Murphy Is Everywhere ��������������������������������������������������������������������������������������������������������������������������������������� 383
Job Boards ��������������������������������������������������������������������������������������������������������������������������������384
Summary �����������������������������������������������������������������������������������������������������������������������������������385
Index ���������������������������������������������������������������������������������������������������������������������������������387
xvii
About the Author
Janine Suvak was born and raised in the San Francisco Bay Area and studied electrical engineering at CalTech before going on to serve in the U.S. military. There she worked with avionics systems before switching to medical school, only to return to the military as a flight surgeon. Her interest in programming began in junior high with the Apple II, and she applied this interest throughout her military career to various projects, from the early transition of maintenance parts tracking from paper to computers, to writing personnel tracking database systems and proprietary electronic medical records programs before such products were commercially available.
After leaving military service, Janine turned her favorite pastime of programming into a full-time freelance career, initially developing iOS applications before delving into Android development. Her recent endeavors resulted in winning entries for two of five categories in the Samsung Hope for the Children competition for educational games development, which are to be released later this year. She is particularly interested in 3D games and augmented reality.
xix
About the Technical Reviewer
Graham McAllister was first involved in the games industry when he was 15 years old, drawing loading screens in Deluxe Paint for 16-bit games on the Amiga.
He originally enrolled in university to study electronic engineering; however, this only lasted two weeks when he realized that he didn’t think in terms of hardware at all. Transferring to a degree in software engineering, he immediately felt at home, going on to earn a PhD in computer graphics.
After a brief period as a software engineer in telecoms, he returned to academia as a lecturer in music technology at Queens University Belfast. In early 2006 he went on a three-month secondment to a little-known company in Copenhagen developing an exciting new games engine. It was called Unity. Graham wrote the first FPS tutorials, which many people would use to learn the essentials of making games in Unity.
Realizing that making games isn’t just about the technology, but also the human aspects, he moved to the University of Sussex where he was a senior lecturer in Human-Computer Interaction.
Leaving academia behind in January 2012, Graham founded Player Research, a user research and playtesting studio based in Brighton, UK. Player Research works with video game studios at all stages of game development, from concept through release, providing evidence to support design decisions and deliver the best possible player experience. Their clients include Sony, NaturalMotion Games, Mind Candy, Jagex, Ninja Theory, Madfinger Games, Hutch Games, and Channel4, among many others.
Graham also writes the column on user research for EDGE, www.edge-online.com/author/gmcallister.
xxi
Acknowledgments
I would like to thank the entire Apress team involved in the production of this book. Special thanks to Louise Corrigan and Christine Ricketts for their support and encouragement, James Fraleigh, and especially technical reviewer Graham McAllister for his thorough review and helpful suggestions.
xxiii
Introduction
If you have the desire to create video games but have no experience with programming or game development, this is the book for you. Unity is a powerful game development ecosystem for creating 2D and 3D games. With its basic but still powerful free version, Unity has blasted away the barriers to learning game development. While the Unity editor is visual and intuitive in nature, you will have to learn to script in order to complete your game. Not to fear—all you need is a computer, an Internet connection, and motivation. This book is written for the complete beginner in both game development and programming.
Scripting, programming, and coding are synonymous terms that all refer to the process of writing computer code to direct some portion of the game behavior. While it may seem a little daunting, as with any other endeavor you simply start at the beginning and learn one thing at a time. You can’t make a game without writing some code, but creating the code is only a part of the game development process. You’ll have just as much fun learning to find and use characters, animations, special effects, and other assets as you will writing code for directing their interaction within your game.
Unity Technologies, the maker of Unity3D, provides excellent support documentation that most often includes working sample code. When I was a beginner developer, whether in mobile apps or video games, I found that almost all of the documentation was just about as clear and useful as if it were written in hieroglyphics—pretty much incomprehensible. I was stuck in a hole where I needed documentation on how to use the documentation!
The purpose of this book is to bridge this initial gap—to give you a foundation in programming within the context of using Unity to make a simple game, while connecting what you are learning to the relevant information found in the documentation, and even to using some of the sample code. This book is meant as a launching pad: by the time you are through working on its examples, you should be able to confidently build on your newfound knowledge and skills with the many resources introduced to you throughout this book.
With that in mind, read the chapters in order. After an introduction to the basics of programming concepts and to the Unity editor in the first few chapters, the subsequent projects will begin building upon each other from one chapter to the next. To get the most out of this book, follow along and complete the projects step by step from scratch. The best way to learn and (just as important) retain what you are learning is by doing. Besides, it’s fun and definitely more satisfying to do it yourself.
xxiv Introduction
The game development industry is in constant motion, and with over half of all game developers using Unity, from individuals to large studios, you can bet that Unity Technologies is constantly pushing the envelope to provide better tools and game engine performance. In particular, during the time this book was written, several radical changes were made affecting animations, particle systems, and better assets for rapid prototyping. Any changes affecting the instructions in this book from Unity or elsewhere will be noted in the Errata section of the book’s Apress web page, www.apress.com/9781430265863.
Sometimes it is helpful to have the finished project as a reference. You will be able to find the source code and finished projects for the examples in this book under the Source Code/Downloads tab of its Apress web page.
The best way to learn how to make games is by making games, so turn the page to get started!