beginning 3d game development with unity 4 - springerbeginning 3d game development with unity 4:...
TRANSCRIPT
Beginning 3D Game Development with Unity 4: All-in-One, Multi-Platform Game Development
Sue Blackman
Beginning 3D Game Development with Unity 4: All-in-One, Multi-Platform Game Development
Copyright © 2013 by Sue Blackman
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
LucasArts and the LucasArts logo are registered trademarks of Lucasfilm Ltd. © 1992-1998 Lucasfilm Entertainment Company Ltd. or Lucasfilm Ltd. & Ò TM, as indicated. All rights reserved.
ISBN-13 (pbk): 978-1-4302-4899-6
ISBN-13 (electronic): 978-1-4302-4900-9
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.
President and Publisher: Paul ManningLead Editor: Michelle LowmanDevelopment Editor: Douglas PundickTechnical Reviewer: Robert ReedEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick,
Jonathan Hassell, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Tom Welsh
Coordinating Editor: Christine RickettsCopy Editor: Michael G. LaraqueCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC, 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.
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.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com. You will be required to answer questions pertaining to this book in order to successfully download the code.
For Phillip, who kept reminding me about how long I had been talking about writing this book.
v
Contents at a Glance
About the Author ��������������������������������������������������������������������������������������������������������������� xix
About the Technical Reviewer ������������������������������������������������������������������������������������������� xxi
Acknowledgments ����������������������������������������������������������������������������������������������������������� xxiii
Introduction ���������������������������������������������������������������������������������������������������������������������� xxv
Chapter 1: Introduction to Game Development ■ ������������������������������������������������������������������1
Chapter 2: Unity UI Basics—Getting Started ■ �������������������������������������������������������������������23
Chapter 3: Scripting: Getting Your Feet Wet ■ ��������������������������������������������������������������������63
Chapter 4: Terrain Generation: Creating a Test Environment ■ �������������������������������������������85
Chapter 5: Navigation and Functionality ■ �����������������������������������������������������������������������139
Chapter 6: Cursor Control ■ ����������������������������������������������������������������������������������������������189
Chapter 7: Imported Assets ■ �������������������������������������������������������������������������������������������223
Chapter 8: Action Objects ■ ����������������������������������������������������������������������������������������������263
Chapter 9: Managing State ■ ��������������������������������������������������������������������������������������������299
Chapter 10: Exploring Transitions ■ ���������������������������������������������������������������������������������345
Chapter 11: Physics and Special Effects ■ ����������������������������������������������������������������������391
Chapter 12: Message Text ■ ���������������������������������������������������������������������������������������������451
Chapter 13: Inventory Logic ■ ������������������������������������������������������������������������������������������483
Chapter 14: Managing the Inventory ■ �����������������������������������������������������������������������������511
Chapter 15: Dialogue Trees ■ ��������������������������������������������������������������������������������������������551
■ Contents at a GlanCe
vi
Chapter 16: Mecanim and Characters ■ ���������������������������������������������������������������������������581
Chapter 17: Game Environment ■ �������������������������������������������������������������������������������������637
Chapter 18: Setting Up the Game ■ ����������������������������������������������������������������������������������679
Chapter 19: Menus and Levels ■ ��������������������������������������������������������������������������������������725
Index ���������������������������������������������������������������������������������������������������������������������������������767
vii
Contents
About the Author ��������������������������������������������������������������������������������������������������������������� xix
About the Technical Reviewer ������������������������������������������������������������������������������������������� xxi
Acknowledgments ����������������������������������������������������������������������������������������������������������� xxiii
Introduction ���������������������������������������������������������������������������������������������������������������������� xxv
Chapter 1: Introduction to Game Development ■ ������������������������������������������������������������������1
The Adventure Genre ���������������������������������������������������������������������������������������������������������������������1
Text Adventure Games ������������������������������������������������������������������������������������������������������������������������������������������� 1
Graphical Adventure ���������������������������������������������������������������������������������������������������������������������������������������������� 2
Fast Forward to Real Time ������������������������������������������������������������������������������������������������������������������������������������� 3
Designing Your Game ��������������������������������������������������������������������������������������������������������������������6
Defining a Style ����������������������������������������������������������������������������������������������������������������������������������������������������� 7
Compartmentalizing Environments ����������������������������������������������������������������������������������������������������������������������� 8
First-Person or Third? �������������������������������������������������������������������������������������������������������������������������������������������� 9
Animation �������������������������������������������������������������������������������������������������������������������������������������������������������������� 9
Content ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 10
Managing Your Project ����������������������������������������������������������������������������������������������������������������11
Multiple Roles ������������������������������������������������������������������������������������������������������������������������������������������������������ 11
Choosing the Game Engine ���������������������������������������������������������������������������������������������������������������������������������� 11
The Requirements ����������������������������������������������������������������������������������������������������������������������������������������������� 11
Tips for Completing Your First Game ������������������������������������������������������������������������������������������������������������������� 12
New to Real Time vs� Pre-render�������������������������������������������������������������������������������������������������14
Terms and Concepts �������������������������������������������������������������������������������������������������������������������������������������������� 15
Summary �������������������������������������������������������������������������������������������������������������������������������������21
■ Contents
viii
Chapter 2: Unity UI Basics—Getting Started ■ �������������������������������������������������������������������23
Installing Unity and Starting Up ���������������������������������������������������������������������������������������������������23
Loading or Creating a New Project or Scene ������������������������������������������������������������������������������23
The Layout ����������������������������������������������������������������������������������������������������������������������������������27
Scene View ��������������������������������������������������������������������������������������������������������������������������������������������������������� 28
Game Window ������������������������������������������������������������������������������������������������������������������������������������������������������ 29
Hierarchy View ����������������������������������������������������������������������������������������������������������������������������������������������������� 30
Project View ��������������������������������������������������������������������������������������������������������������������������������������������������������� 30
Inspector �������������������������������������������������������������������������������������������������������������������������������������������������������������� 30
Toolbar �����������������������������������������������������������������������������������������������������������������������������������������31
Menus������������������������������������������������������������������������������������������������������������������������������������������32
File ���������������������������������������������������������������������������������������������������������������������������������������������������������������������� 33
Edit ���������������������������������������������������������������������������������������������������������������������������������������������������������������������� 33
Assets ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 34
GameObject �������������������������������������������������������������������������������������������������������������������������������������������������������� 34
Component ��������������������������������������������������������������������������������������������������������������������������������������������������������� 34
Window ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 34
Help ��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 34
Creating Simple Objects ��������������������������������������������������������������������������������������������������������������34
Selecting and Focusing ���������������������������������������������������������������������������������������������������������������40
Transforming Objects ������������������������������������������������������������������������������������������������������������������41
Snaps ������������������������������������������������������������������������������������������������������������������������������������������44
Vertex Snaps �������������������������������������������������������������������������������������������������������������������������������������������������������� 45
Scene Gizmo �������������������������������������������������������������������������������������������������������������������������������46
Non-Snap Alignment �������������������������������������������������������������������������������������������������������������������������������������������� 47
Lights ������������������������������������������������������������������������������������������������������������������������������������������48
■ Contents
ix
3D Objects �����������������������������������������������������������������������������������������������������������������������������������55
Meshes ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 55
Sub-Objects of a Mesh ���������������������������������������������������������������������������������������������������������������������������������������� 56
Mapping ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 57
Materials �������������������������������������������������������������������������������������������������������������������������������������61
Summary �������������������������������������������������������������������������������������������������������������������������������������61
Chapter 3: Scripting: Getting Your Feet Wet ■ ��������������������������������������������������������������������63
What Is a Script? �������������������������������������������������������������������������������������������������������������������������63
Components of a Script ���������������������������������������������������������������������������������������������������������������64
Functions ������������������������������������������������������������������������������������������������������������������������������������������������������������� 64
Variables �������������������������������������������������������������������������������������������������������������������������������������������������������������� 72
Picking an Object in the Game����������������������������������������������������������������������������������������������������������������������������� 76
Conditionals and State ����������������������������������������������������������������������������������������������������������������81
Deconstructing the Conditional ��������������������������������������������������������������������������������������������������������������������������� 83
Order of Evaluation ����������������������������������������������������������������������������������������������������������������������84
Summary �������������������������������������������������������������������������������������������������������������������������������������84
Chapter 4: Terrain Generation: Creating a Test Environment ■ �������������������������������������������85
Creating Environments ����������������������������������������������������������������������������������������������������������������85
Creating a Terrain Object ������������������������������������������������������������������������������������������������������������������������������������� 86
Flythrough Scene Navigation ������������������������������������������������������������������������������������������������������������������������������� 90
The Terrain Engine �����������������������������������������������������������������������������������������������������������������������90
Topology �������������������������������������������������������������������������������������������������������������������������������������������������������������� 91
Painting Textures ������������������������������������������������������������������������������������������������������������������������������������������������� 95
Trees �������������������������������������������������������������������������������������������������������������������������������������������������������������������� 99
Terrain Extras ����������������������������������������������������������������������������������������������������������������������������������������������������� 122
Creating Your Own Terrain Assets ���������������������������������������������������������������������������������������������������������������������� 128
Terrain Settings ������������������������������������������������������������������������������������������������������������������������������������������������� 130
Shadows ������������������������������������������������������������������������������������������������������������������������������������132
Fog ��������������������������������������������������������������������������������������������������������������������������������������������137
Summary �����������������������������������������������������������������������������������������������������������������������������������138
■ Contents
x
Chapter 5: Navigation and Functionality ■ �����������������������������������������������������������������������139
Navigation ���������������������������������������������������������������������������������������������������������������������������������139
Arrow Navigation and Input ������������������������������������������������������������������������������������������������������������������������������� 146
Fun with Platforms �������������������������������������������������������������������������������������������������������������������������������������������� 159
Collision Walls ���������������������������������������������������������������������������������������������������������������������������������������������������� 168
Your First Build �������������������������������������������������������������������������������������������������������������������������������������������������� 182
Summary �����������������������������������������������������������������������������������������������������������������������������������188
Chapter 6: Cursor Control ■ ����������������������������������������������������������������������������������������������189
Cursor Visibility ��������������������������������������������������������������������������������������������������������������������������189
Custom Cursors �������������������������������������������������������������������������������������������������������������������������192
GUI Texture Cursor ���������������������������������������������������������������������������������������������������������������������192
Texture Importer ������������������������������������������������������������������������������������������������������������������������������������������������ 193
Color Cues ��������������������������������������������������������������������������������������������������������������������������������������������������������� 203
Cursor Position �������������������������������������������������������������������������������������������������������������������������������������������������� 203
Hardware Cursor �����������������������������������������������������������������������������������������������������������������������205
UnityGUI Cursor �������������������������������������������������������������������������������������������������������������������������206
Object-to-Object Communication ����������������������������������������������������������������������������������������������209
Mouseover Cursor Changes ������������������������������������������������������������������������������������������������������210
Distance Check �������������������������������������������������������������������������������������������������������������������������������������������������� 217
Quick Publish�����������������������������������������������������������������������������������������������������������������������������220
Object Reaction to Mouseover ��������������������������������������������������������������������������������������������������221
Summary �����������������������������������������������������������������������������������������������������������������������������������222
Chapter 7: Imported Assets ■ �������������������������������������������������������������������������������������������223
3D Art Assets �����������������������������������������������������������������������������������������������������������������������������223
Importing the Art Assets ������������������������������������������������������������������������������������������������������������������������������������ 224
Import Settings ��������������������������������������������������������������������������������������������������������������������������228
Import Settings: Model �������������������������������������������������������������������������������������������������������������������������������������� 228
Import Settings: Rig ������������������������������������������������������������������������������������������������������������������������������������������� 234
Parenting ����������������������������������������������������������������������������������������������������������������������������������������������������������� 237
Import Settings: Animations ������������������������������������������������������������������������������������������������������������������������������ 240
■ Contents
xi
Setting Up Materials ������������������������������������������������������������������������������������������������������������������247
The Keys ������������������������������������������������������������������������������������������������������������������������������������������������������������ 247
The Chest ����������������������������������������������������������������������������������������������������������������������������������������������������������� 252
The Flower ��������������������������������������������������������������������������������������������������������������������������������������������������������� 254
Shadows ������������������������������������������������������������������������������������������������������������������������������������257
Static Objects and Shadows ������������������������������������������������������������������������������������������������������������������������������ 258
Summary �����������������������������������������������������������������������������������������������������������������������������������262
Chapter 8: Action Objects ■ ����������������������������������������������������������������������������������������������263
Colliders ������������������������������������������������������������������������������������������������������������������������������������263
Triggering Animation �����������������������������������������������������������������������������������������������������������������268
Triggering Complex Hierarchies ������������������������������������������������������������������������������������������������������������������������� 269
Adding Sound F/X ����������������������������������������������������������������������������������������������������������������������272
Setting Up a Two-State Animation ���������������������������������������������������������������������������������������������277
Using Unity’s Animation View ����������������������������������������������������������������������������������������������������279
Changing Imported Animations ������������������������������������������������������������������������������������������������������������������������� 290
Triggering Another Object’s Animations ������������������������������������������������������������������������������������295
Limitations ���������������������������������������������������������������������������������������������������������������������������������297
Summary �����������������������������������������������������������������������������������������������������������������������������������298
Chapter 9: Managing State ■ ��������������������������������������������������������������������������������������������299
Identifying the Action Objects ���������������������������������������������������������������������������������������������������300
Introducing the Test Plot ����������������������������������������������������������������������������������������������������������������������������������� 300
Walk-through ����������������������������������������������������������������������������������������������������������������������������������������������������� 300
Action Objects ���������������������������������������������������������������������������������������������������������������������������������������������������� 301
Stunt Doubles ���������������������������������������������������������������������������������������������������������������������������������������������������� 301
Developing a State Machine ������������������������������������������������������������������������������������������������������301
Components of a State Machine ������������������������������������������������������������������������������������������������������������������������ 302
The Pick Event ��������������������������������������������������������������������������������������������������������������������������������������������������� 302
Separating State Metadata and Transition Action ���������������������������������������������������������������������������������������������� 304
Defining State and Transition ����������������������������������������������������������������������������������������������������������������������������� 304
■ Contents
xii
Visualizing Transitions ��������������������������������������������������������������������������������������������������������������������������������������� 305
Organizing the Information �������������������������������������������������������������������������������������������������������������������������������� 307
Lookup Table �����������������������������������������������������������������������������������������������������������������������������309
Evaluating the Results ��������������������������������������������������������������������������������������������������������������������������������������� 310
Parsing a String ������������������������������������������������������������������������������������������������������������������������������������������������� 311
Game Notes ������������������������������������������������������������������������������������������������������������������������������������������������������� 312
The Object Lookup Script ����������������������������������������������������������������������������������������������������������313
Action-Related Messages����������������������������������������������������������������������������������������������������������322
Object Metadata ������������������������������������������������������������������������������������������������������������������������323
State-Related Metadata ������������������������������������������������������������������������������������������������������������������������������������� 324
Processing the Pick Event ��������������������������������������������������������������������������������������������������������������������������������� 327
Adding the Metadata ����������������������������������������������������������������������������������������������������������������������������������������� 330
Activating the State Engine ������������������������������������������������������������������������������������������������������������������������������� 332
Refining the Pick ����������������������������������������������������������������������������������������������������������������������������������������������� 340
Hiding the Cursor ����������������������������������������������������������������������������������������������������������������������������������������������� 341
Creating a Timer ������������������������������������������������������������������������������������������������������������������������������������������������ 342
Summary �����������������������������������������������������������������������������������������������������������������������������������343
Chapter 10: Exploring Transitions ■ ���������������������������������������������������������������������������������345
Processing the Auxiliary Objects �����������������������������������������������������������������������������������������������345
Handling Object Visibility �����������������������������������������������������������������������������������������������������������348
Using Tags ��������������������������������������������������������������������������������������������������������������������������������������������������������� 349
Visibility ������������������������������������������������������������������������������������������������������������������������������������������������������������� 353
Handling Opacity Changes ��������������������������������������������������������������������������������������������������������������������������������� 355
Developing Non-Keyframed Fades �������������������������������������������������������������������������������������������������������������������� 359
Completing Visibility Changes ��������������������������������������������������������������������������������������������������������������������������� 361
Handling Special Cases �������������������������������������������������������������������������������������������������������������365
Ensuring Player Focus ���������������������������������������������������������������������������������������������������������������371
Blocking Player Input ����������������������������������������������������������������������������������������������������������������������������������������� 371
Directing Player Focus ��������������������������������������������������������������������������������������������������������������������������������������� 374
Exploring Lerp ���������������������������������������������������������������������������������������������������������������������������������������������������� 374
■ Contents
xiii
Camera Match Metadata ����������������������������������������������������������������������������������������������������������������������������������� 380
Refining the Camera Match ������������������������������������������������������������������������������������������������������������������������������� 383
Summary �����������������������������������������������������������������������������������������������������������������������������������389
Chapter 11: Physics and Special Effects ■ ����������������������������������������������������������������������391
Adding New Assets ��������������������������������������������������������������������������������������������������������������������391
Physics ��������������������������������������������������������������������������������������������������������������������������������������395
Physics Basics ��������������������������������������������������������������������������������������������������������������������������������������������������� 396
Combining Physics and Keyframe Animation ����������������������������������������������������������������������������400
Cloth ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 400
RockFall ������������������������������������������������������������������������������������������������������������������������������������������������������������� 409
Introducing Randomness ����������������������������������������������������������������������������������������������������������������������������������� 413
Finding the Crystal��������������������������������������������������������������������������������������������������������������������������������������������� 415
Particle Systems �����������������������������������������������������������������������������������������������������������������������418
Shuriken Basics ������������������������������������������������������������������������������������������������������������������������������������������������� 420
Particle Types ��������������������������������������������������������������������������������������������������������������������������������������������������� 421
Pouring Particles ����������������������������������������������������������������������������������������������������������������������������������������������� 425
Particle Groups �������������������������������������������������������������������������������������������������������������������������������������������������� 429
Other Special Effects �����������������������������������������������������������������������������������������������������������������439
Creating the Laser Beam ����������������������������������������������������������������������������������������������������������������������������������� 439
Building the Laser Device ���������������������������������������������������������������������������������������������������������������������������������� 440
Activating the Laser ������������������������������������������������������������������������������������������������������������������������������������������� 442
Creating the UV Animator Script ������������������������������������������������������������������������������������������������������������������������ 444
Applying the UV Animator Script ������������������������������������������������������������������������������������������������������������������������ 445
Summary �����������������������������������������������������������������������������������������������������������������������������������449
Chapter 12: Message Text ■ ���������������������������������������������������������������������������������������������451
GUI Skin �������������������������������������������������������������������������������������������������������������������������������������451
OS Font Support ������������������������������������������������������������������������������������������������������������������������������������������������ 454
Text Visibility ������������������������������������������������������������������������������������������������������������������������������462
■ Contents
xiv
Using the Object Metadata ��������������������������������������������������������������������������������������������������������465
Tweaking the Text ���������������������������������������������������������������������������������������������������������������������������������������������� 468
Activating the Action Message �������������������������������������������������������������������������������������������������������������������������� 472
A Final Refinement �������������������������������������������������������������������������������������������������������������������������������������������� 480
Summary �����������������������������������������������������������������������������������������������������������������������������������481
Chapter 13: Inventory Logic ■ ������������������������������������������������������������������������������������������483
Using Layers ������������������������������������������������������������������������������������������������������������������������������483
Creating the Inventory Screen ���������������������������������������������������������������������������������������������������487
Adding Inventory Icons ��������������������������������������������������������������������������������������������������������������495
2D Object Handling �������������������������������������������������������������������������������������������������������������������������������������������� 500
Cursor-Based Responses ����������������������������������������������������������������������������������������������������������������������������������� 506
Dropping Cursors in the 3D Scene ��������������������������������������������������������������������������������������������507
Summary �����������������������������������������������������������������������������������������������������������������������������������510
Chapter 14: Managing the Inventory ■ �����������������������������������������������������������������������������511
Organizing the Inventory Objects ����������������������������������������������������������������������������������������������511
Inventory Layout ������������������������������������������������������������������������������������������������������������������������522
Inventory Overflow ��������������������������������������������������������������������������������������������������������������������526
Setting the Limits ����������������������������������������������������������������������������������������������������������������������531
Inventory Grid Limits ����������������������������������������������������������������������������������������������������������������������������������������� 532
Arrow Limits ������������������������������������������������������������������������������������������������������������������������������������������������������ 536
Adding and Removing Objects from Inventory ��������������������������������������������������������������������������538
Removing Objects from Inventory ��������������������������������������������������������������������������������������������������������������������� 539
Adding Objects to Inventory������������������������������������������������������������������������������������������������������������������������������� 541
Combining Items ������������������������������������������������������������������������������������������������������������������������������������������������ 542
Wrong Picks ������������������������������������������������������������������������������������������������������������������������������������������������������� 545
Revisiting the Action Objects ����������������������������������������������������������������������������������������������������������������������������� 549
Summary �����������������������������������������������������������������������������������������������������������������������������������550
■ Contents
xv
Chapter 15: Dialogue Trees ■ ��������������������������������������������������������������������������������������������551
The Scenario �����������������������������������������������������������������������������������������������������������������������������551
Setting Up the New GUI Skin ����������������������������������������������������������������������������������������������������������������������������� 552
Starting a Conversation �������������������������������������������������������������������������������������������������������������552
Filtering Topics �������������������������������������������������������������������������������������������������������������������������������������������������� 555
Getting Answers ������������������������������������������������������������������������������������������������������������������������������������������������ 557
Branching the Conversation ������������������������������������������������������������������������������������������������������������������������������ 566
Summary �����������������������������������������������������������������������������������������������������������������������������������580
Chapter 16: Mecanim and Characters ■ ���������������������������������������������������������������������������581
Adding Characters ���������������������������������������������������������������������������������������������������������������������581
Importing the Characters ����������������������������������������������������������������������������������������������������������������������������������� 581
Setting Up the Avatars ��������������������������������������������������������������������������������������������������������������������������������������� 583
Defining the Animation Clips ����������������������������������������������������������������������������������������������������������������������������� 587
Controlling the Characters���������������������������������������������������������������������������������������������������������590
The Animator Controller ������������������������������������������������������������������������������������������������������������������������������������� 590
Making Changes ������������������������������������������������������������������������������������������������������������������������������������������������ 592
Transitions ��������������������������������������������������������������������������������������������������������������������������������������������������������� 593
Scripting the Action ������������������������������������������������������������������������������������������������������������������������������������������� 597
Blend Trees �������������������������������������������������������������������������������������������������������������������������������������������������������� 600
Layers and Layer Masks ������������������������������������������������������������������������������������������������������������������������������������ 603
Physics �������������������������������������������������������������������������������������������������������������������������������������������������������������� 606
NPC Controller ���������������������������������������������������������������������������������������������������������������������������607
Tweaking for Game �������������������������������������������������������������������������������������������������������������������������������������������� 608
Synchronizing Audio and Animation States ������������������������������������������������������������������������������������������������������� 611
Combining Characters and Dialogue �����������������������������������������������������������������������������������������615
Hooking Up the Dialogue to the Characters ������������������������������������������������������������������������������������������������������� 615
Communicating with the Dialogue Manager �����������������������������������������������������������������������������630
■ Contents
xvi
Wrapping Up the Test Scene �����������������������������������������������������������������������������������������������������631
Preparing the ObjectLookup ������������������������������������������������������������������������������������������������������������������������������ 632
Adding the NPCs to the Game ���������������������������������������������������������������������������������������������������������������������������� 633
Summary �����������������������������������������������������������������������������������������������������������������������������������636
Chapter 17: Game Environment ■ �������������������������������������������������������������������������������������637
Adding the Final Assets ������������������������������������������������������������������������������������������������������������637
Preparing for the New Assets ���������������������������������������������������������������������������������������������������������������������������� 637
Revisiting Materials and Lightmapping ������������������������������������������������������������������������������������������������������������� 639
Using Imported Lightmaps in Beast ������������������������������������������������������������������������������������������������������������������� 646
Accessing the Lower Levels of the Temple �������������������������������������������������������������������������������������������������������� 647
Setting Up the Shrines ��������������������������������������������������������������������������������������������������������������655
Hydrating the Water Shrine�������������������������������������������������������������������������������������������������������������������������������� 656
Illuminating the Light Shrine ����������������������������������������������������������������������������������������������������������������������������� 657
Setting Up the Maze ������������������������������������������������������������������������������������������������������������������658
Examining the Maze Geometry �������������������������������������������������������������������������������������������������������������������������� 658
Managing the Maze Functionality ���������������������������������������������������������������������������������������������������������������������� 659
Checking for Drop Point Access������������������������������������������������������������������������������������������������������������������������� 661
Playing the Odds ������������������������������������������������������������������������������������������������������������������������������������������������ 669
Creating an “Easter Egg” ���������������������������������������������������������������������������������������������������������������������������������� 675
Summary �����������������������������������������������������������������������������������������������������������������������������������677
Chapter 18: Setting Up the Game ■ ����������������������������������������������������������������������������������679
Finishing the Main Level �����������������������������������������������������������������������������������������������������������679
Reading Material ����������������������������������������������������������������������������������������������������������������������������������������������� 679
Finding the Golden Sleeve ��������������������������������������������������������������������������������������������������������������������������������� 684
Raft Ride ������������������������������������������������������������������������������������������������������������������������������������������������������������ 687
Locating the Crystal ������������������������������������������������������������������������������������������������������������������������������������������� 691
Access to the Temple ����������������������������������������������������������������������������������������������������������������������������������������� 694
Meeting Gimbok ������������������������������������������������������������������������������������������������������������������������������������������������ 699
Meeting Kahmi �������������������������������������������������������������������������������������������������������������������������������������������������� 706
Map of Tunnels �������������������������������������������������������������������������������������������������������������������������������������������������� 711
■ Contents
xvii
Acquiring a Light Source ����������������������������������������������������������������������������������������������������������������������������������� 713
The Tunnels �������������������������������������������������������������������������������������������������������������������������������������������������������� 716
The Final Level ��������������������������������������������������������������������������������������������������������������������������722
Examining the Final Level ���������������������������������������������������������������������������������������������������������������������������������� 723
Summary �����������������������������������������������������������������������������������������������������������������������������������724
Chapter 19: Menus and Levels ■ ��������������������������������������������������������������������������������������725
Game Menus �����������������������������������������������������������������������������������������������������������������������������725
The MenuManager Script ���������������������������������������������������������������������������������������������������������������������������������� 726
The Main Menu �������������������������������������������������������������������������������������������������������������������������������������������������� 728
Confirm Dialog ��������������������������������������������������������������������������������������������������������������������������������������������������� 737
Credits �������������������������������������������������������������������������������������������������������������������������������������������������������������� 740
Tracking Menu Mode ����������������������������������������������������������������������������������������������������������������������������������������� 741
Suspending Navigation and Scene Interaction ������������������������������������������������������������������������������������������������� 742
Audio �����������������������������������������������������������������������������������������������������������������������������������������744
Sound FX ����������������������������������������������������������������������������������������������������������������������������������������������������������� 744
Ambient Sound �������������������������������������������������������������������������������������������������������������������������������������������������� 745
Music ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 746
Voice Volume ����������������������������������������������������������������������������������������������������������������������������������������������������� 747
Final Level Menu ����������������������������������������������������������������������������������������������������������������������������������������������� 747
Level Save and Load �����������������������������������������������������������������������������������������������������������������748
Saving with PlayerPrefs ������������������������������������������������������������������������������������������������������������������������������������� 748
Saving to Text Files �������������������������������������������������������������������������������������������������������������������������������������������� 748
Saving the Game Data ��������������������������������������������������������������������������������������������������������������������������������������� 752
Loading the Saved Data ������������������������������������������������������������������������������������������������������������������������������������� 754
Traveling Between Levels ����������������������������������������������������������������������������������������������������������761
Start Menu ���������������������������������������������������������������������������������������������������������������������������������762
Final Tasks ���������������������������������������������������������������������������������������������������������������������������������763
Publishing ���������������������������������������������������������������������������������������������������������������������������������������������������������� 764
Summary �����������������������������������������������������������������������������������������������������������������������������������766
Index ���������������������������������������������������������������������������������������������������������������������������������767
xix
About the Author
Sue Blackman is a 3D artist and interactive applications author and instructor based in Southern California. She has taught 3ds Max and game classes for artists for well over ten years in top-rated community colleges and private schools, such as The Art Institute of California, and has been lead 3D artist on games for Activision, through one of its subsidiaries. Blackman has worked in the industry for several years, helping to train Fortune 1000 companies, such as Boeing, Raytheon, and Northrop Grumman, to create serious games and training applications with game-based formats. She has been involved with the commercial development of real-time 3D engines for more than ten years and has authored multiple training-video courses for the Unity engine for Lynda.com. She is an avid tutorial writer and has created both tutorials, as a contributing author, and artwork for various 3ds Max books over the years, as well as training manuals for 3D authoring applications for serious games. She has also written for ACM Siggraph on serious games, one of her favorite topics. You can visit Blackman’s web site at www.3dadventurous.com or contact her through the Unity Forum.
xxi
About the Technical Reviewer
Robert Reed, who currently resides in Phoenix, Arizona, where he attained a bachelor’s degree in game design from Collins College, was first introduced to the Unity3D game engine in 2010, when trying to decide what to use for his graduation game project, and he has used it ever since. JavaScript is his primary language when using the Unity3D game engine, but in addition to JavaScript, he has become a bit proficient in a few other programming and scripting languages, including Java, C, C++, C#, ActionScript 3.0, and Lua.
xxiii
Acknowledgments
Thanks go out to Gabriel Acosta and Jenny Wang, who helped brainstorm much of the game play for the book’s project. I owe special thanks to Jenny for the start menu design and stele idea, as well as the 3DAventurous web site.
Thanks also to Bryan Taylor and Sean McCurry of Binary Sonata Studios for the temple music and camera match code.
Thanks are also in order for Evalyn Hernandez, Zach Sarachman, Rosendo Rojas, Mario Im, and Pablo Ortega, who got roped into recording some of the character dialogue. A special thanks goes out to Erik Toraason, who channeled his “inner Yoda” for the Gimbok character.
Thank you all for taking time out from your own game projects to contribute to mine. Your assets, ideas, and enthusiasms were much appreciated.
xxv
Introduction
Why Write This BookReal-time 3D games have been around for well over ten years now. We’ve all played them, created assets in the style of our favorites, and maybe even “mod”ed a few of them. But with the Unity game engine leaping ahead in its effort to provide a free or low-cost means of authoring for desktop, mobile, or console games, the only barrier left to creating your own games is your level of commitment and the number of hours you are willing or able to devote.
Times have changed. 3D has become affordable not only in the movie industry, as seen by the number of titles featuring CG (computer graphics), but also in the game industry, where we’ve seen a shift in casual games from 2D to a 3D format. With Unity’s bold move to offer a robustly featured free version of its engine, a radical change in the pricing models of the high-end engines has rocked the industry. The cost of the engine is no longer a barrier to taking your game from a nebulous idea to a working prototype and even on to a marketable product.
Whether your interest is in casual games or you have more ambitious aims, if you have no previous scripting experience but are eager to bring your art assets and story to life, this may be just the book to get you under way. In today’s modern game engines, the heavy programming is handled by the engine itself, so the logic and game play can be scripted by those with more creativity than traditional programming knowledge.
In this book, I will approach game creation and design decisions from a 3D artist’s view, taking the logic and scripting in small pieces, while introducing artists, budding game designers, and novice programmers to real-time game engine concepts at the same time.
This book is written in a project-based format, so you will not only end up with a playable game and scripting resources you can reuse for other games, but you will experience typical design decisions that have to be addressed throughout the process. You will create your game by starting with the basics and refining it as you add functionality; I will explain the logic behind certain choices and real-time game concepts as you go along.
The project for this book is based on a first-person point-and-click adventure game, complete with inventory, state management, load/save functionality, and a strong emphasis on the visual aspects of game creation. Even if you are more of a first-person-shooter-type game enthusiast, you will be able to see how including entertaining or interesting tasks and features can enhance your favorite genre.
The aim of this project is to introduce you to a logical progression of design decisions and problem solving that will be of value well beyond the scope of the adventure game genre. It provides a framework and a methodology for creating and, more important, finishing your own game. You will be going beyond the basic Unity functionality and use of standard assets to investigate topics rarely covered in online tutorials or books. All of the necessary art assets to complete the project are provided.
Author’s NoteSeveral years ago, after taking my class through a race game, a first-person shooter, and a platform jumper, I decided that the last mini-project would be a classic adventure game. Much to my surprise, the class got quite motivated with all of the design decisions and logic involved. As there were no existing tutorials on this genre at the time, we created the game from the ground up, borrowing scripts from the previous projects, creating several of our own, and drawing heavily on the knowledge base of the Unity community. This book grew out of that experience, both to fill a need and to share the adventure with others.
■ IntroduCtIon
xxvi
Contemporary Adventure GamesFor this project, you will be creating a variation on the classic point-and-click adventure game. The adventure genre, of which there are many variations, is an ideal starting place for artists and others with little or no scripting experience. The story, art assets, and animation are major components of this type of game, and the slower pace allows more freedom from script optimization, as split-second player reaction time is not required. If your only goal is to create a first-person-shooter-type game, this book may not be for you. Conversely, if your tastes are more eclectic, you may find all sorts of information that can be applied to your current genre of choice.
With Unity 4.0, the addition of Mecanim provides an ideal opportunity to introduce another common feature of the adventure genre—characters and dialogue trees. That you can do so without being an expert on character animation makes it even more fun. The more substantial incorporation of physics and various special effects this time around has also contributed to pushing this edition of the book’s project from “classic” to “contemporary.”
One of the most enjoyable components of the adventure game is the collection and use of an odd assortment of objects. Because of the importance of inventory and state management, several chapters are dedicated to their design and implementation. Neophyte Unity developers often ask how to implement these features in community forums, but they rarely receive answers, owing to the scope of the topic. By the end of this book, you will be armed with the scripts, concepts, and experience to be able to take the knowledge beyond this genre.
Interactive adventure games are also ideal for indie developers, and they appeal to a broad range of players. FireProof Games’ The Room, authored in Unity, was one of the top-rated mobile games of the year, proving that Unity has taken its commitment to the mobile and casual gaming community very seriously.
About the Unity Game EngineUnity is the perfect choice for small studios, indie developers, and those of us who have always wanted to make our own games. Its large user base (more than 1.2 million in the summer of 2013) and extremely active user community allows everyone from newbies to seasoned veterans to get answers and share information quickly.
Unity provides an excellent entry point into game development, balancing features and functionality with price point. The free version of Unity allows people to experiment, learn, develop, and sell games before committing any of their hard-earned cash. Unity’s very affordable, feature-packed Pro version is royaltyfree, allowing people to make and sell games with the very low overhead essential to the casual games market.
The market for multi-platform games—especially casual games for iPhone and Android—is extremely popular at the moment, and Unity’s commitment to cross-platform delivery is well proven. Originally a Mac-based authoring application that could publish to Mac and Windows, Unity unveiled its Windows version in spring 2009. As expected, it has opened opportunities for PC-based developers and artists. Since that time, Unity has continued to add support for iPhone, Android, iPad, and Wii and is developing support for Xbox 360 and PS3. In spring 2013, Unity Technologies revealed its latest surprise: for Unity free users, the license for iOS, Android, Windows 8 mobile, and Blackberry is now, or will shortly be, free as well.
Early adapters of the Unity engine tended to migrate from Flash and Director, making the scripting environment easily adoptable. While many Unity users have an ActionScript background in making Flash games, it is by no means a prerequisite. There is a lot of material for creating games in Unity, including first-person shooters, racing games, platform jumpers, and the like. Even if your interest lies elsewhere, there are plenty of helpful tips and tricks to be gleaned from even the most unlikely sources. Don’t be afraid to take advantage of the resources available on the Unity web site (www.Unity3D.com), the Unity Forum (forum.unity3d.com), Wiki (www.unifycommunity.com/wiki), UnityAnswers (answers.unity3d.com), and numerous private-party web sites devoted to the Unity engine.
Unity documentation also contains a wealth of valuable information, but, as with any technology that has a unique vocabulary, it’s sometimes hard to find just what you’re looking for. Prefacing your query with Unity or Unity3D and searching the Internet is often the easiest way to find elusive concepts or functionality. You can make use of them via the Help menu, but it is often quicker to take advantage of the online version.
■ IntroduCtIon
xxvii
Will I Have to Learn to Script? You don’t have to be a programmer to make your own game with Unity, but you will have to be able to understand enough of what the scripts do to know what can be tweaked to your advantage or decide if a particular script will suit your needs.
Most game play has to be scripted in Unity, but there are hundreds of scripts already available that can be readily reused. Unity ships with several of the most useful. More can be found by searching the Unity Forum, Wiki, or UnityAnswers. Many forum members will even write bits of script for less adept users. In the Collaboration section of the forum, you can even find scripters looking to trade for art assets. By the end of this book, you should know enough to be able to take advantage of the wealth of material available from the Unity community.
Games, by their very definition, are about interaction; even in games that are largely controlled by physics, logic-driven cause and effect is what differentiates them from linear plot-driven passive media. Even the most “artist friendly” game engines require scripting to move beyond simple environmental walk-throughs. This book’s aim is to familiarize you with scripting a few lines at a time, while providing visual feedback as often as possible. The assumption is that you have not yet decided to learn to script but are happy enough to participate in its creation in a more passive manner.
Scripting Is More About Logic Than Syntax While many people worry that learning a new language will be difficult and intimidating, think of it this way: most people under the age of 35 are quite fluent in texting, which is a valid subset of our native language. To a certain extent, it has its own vocabulary and syntax, and it is very similar to scripting languages, in that it is more of a subset than an entirely new language.
The difference mainly lies in the method used to acquire the “language.” In texting, as with our native language, one doesn’t set out to study and learn the language. Instead, one absorbs, experiments, and eventually becomes competent through repeated contact, trial and error, and a host of other passive means, rather than rote memorization and stressful examination. This book, because it’s about the logic behind game design and creation, treats scripting as an immersive experience that is a sideline to the game-development process. You are free to choose your own amount of involvement with the scripting. Whatever you choose, the main scripts developed in the book will enable you to create or expand upon the final output of the book, the game, with your own ideas and art assets.
That being said, there are only a few important concepts and a handful of keywords you need to know in order to get an idea of what’s being done in a script. Fortunately, most scripters are quite good at including comments explaining what the code is doing, thus making complicated scripts much less daunting to investigate.
Scripts developed in this book will be provided on a per-chapter basis, and the logic behind them explained in the chapters, but I hope that you will display classic adventurer curiosity and take advantage of the scripting explanations to do some experimenting on your own.
What About Math?One of the most common things we hear people say in the 3D industry is, “If I’d known math was going to be so useful, I would have paid more attention in class.” Be that as it may, most artists and designers are not going to want to invest any time in brushing up on their math skills. Don’t worry! My primary goal is to help you create a game. Some of the functionality you will scavenge for your game is easy to use without even knowing how it works. Most of us are quite happy to drive cars without having extensive knowledge of the internal combustion engine. Don’t be afraid to treat scripting the same way!
■ IntroduCtIon
xxviii
Assumptions and PrerequisitesThis book assumes that you are at least somewhat familiar with 3D assets and 3D space, but it does have a short review of the concepts and plenty of tips and tricks throughout.
It assumes that you will not have had much scripting experience (if any at all) but that you’ll be willing to work through it in order to bring your stories to life.
It assumes that, as of yet, you have little or no experience with using the Unity game engine.It also assumes that you are fairly new to the game-making process but have a strong desire to create your own
real-time 3D game.Additionally, this book assumes that if you want to explore genres other than classic point-and-click adventure
games, you will work through the book with a goal of thinking about how to apply the various techniques to get results related to the other genre. In the casual game market, combining elements of adventure games, first-person shooters, and other genres is not only acceptable but makes for some very entertaining results.
What This Book Doesn’t CoverThis book is not about conventional game design; it is more of a precursor, getting you into the habit of analyzing needs and weighing choices. Not only is creating a massive design document intimidating when you are the one who will have to implement everything, but it is likely to be unrealistic until you are more familiar with the engine and your own capabilities. You’re going to build your game up a little bit at a time, prototyping ideas and functionality as you go along.
This is not a book on how to become a programmer, still less on programming best practices. The scripting in this book is designed to ease a non-programmer into the process by providing instant visual feedback as often as possible. While there are usually several ways to attain the same goal, the scripting choices made in this book are the easiest to read and understand from an artist’s or designer’s point of view. In this book, scripting is presented in the way a native speaker learns his or her own language. He or she is surrounded by it, immersed in it, and allowed to tinker with it to slowly gain a basic working knowledge of it. Don’t worry about remembering it all. Some things you will use throughout the project, and others you will be encouraged to take note of for future reference.
Conventions Used in This Book1� Instructions look like this.
Tip ■ Follow this format.
Code looks like this.
PlatformThis book was written using Unity 4.x in a Windows 7 and a Windows 8 environment. Differences for shortcut keys and operating system file storage with Unity on a Mac will be noted throughout the book and are also available through the help files.