Unity Game Development In 24 Hours, Sams Teach Yourself

Transcription

Mike GeigSams Teach YourselfUnity GameDevelopment 24Hoursin800 East 96th Street, Indianapolis, Indiana, 46240 USA

Sams Teach Yourself Unity Game Development in 24 HoursCopyright 2014 by Pearson Education, Inc.All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, ortransmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, withoutwritten permission from the publisher. No patent liability is assumed with respect to the use ofthe information contained herein. Although every precaution has been taken in the preparation ofthis book, the publisher and author assume no responsibility for errors or omissions. Nor is anyliability assumed for damages resulting from the use of the information contained herein.Unity is a trademark of Unity technologies.Kinect is a trademark of Microsoft .PlayStation and PlayStation Move are trademarks of Sony .Wii is a trademark of Nintendo .ISBN-13: 978-0-672-33696-6ISBN-10: 0-672-33696-6Library of Congress Control Number: 2013950040Printed in the United States of AmericaFirst Printing November 2013TrademarksAll terms mentioned in this book that are known to be trademarks or service marks have beenappropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Useof a term in this book should not be regarded as affecting the validity of any trademark or servicemark.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but nowarranty or fitness is implied. The information provided is on an “as is” basis. The author and thepublisher shall have neither liability nor responsibility to any person or entity with respect to anyloss or damages arising from the information contained in this book.Bulk SalesSams Publishing offers excellent discounts on this book when ordered in quantity for bulkpurchases or special sales. For more information, please contactU.S. Corporate and Government or sales outside of the U.S., please contactInternational k TaubExecutive EditorLaura LewinSenior DevelopmentEditorChris ZahnManaging EditorKristy HartProject EditorAndy BeasterCopy EditorKeith ClineIndexerBrad HerrimanProofreaderSheri CainTechnical EditorsTim HarringtonValerie ShipbaughJeff SomersPublishingCoordinatorOlivia BasegioInterior DesignerGary AdairCover DesignerMark ShirarCompositorGloria Schurick

Contents at a GlancePreface .xiHOUR 1 Introduction to Unity . 1HOUR 2 Game Objects . 23HOUR 3 Models, Materials, and Textures . 37HOUR 4 Terrain . 51HOUR 5 Environments . 65HOUR 6 Lights and Cameras . 81HOUR 7 Game 1: Amazing Racer . 101HOUR 8 Scripting Part 1 . 117HOUR 9 Scripting Part 2 . 137HOUR 10 Collision . 155HOUR 11 Game 2: Chaos Ball . 167HOUR 12 Prefabs . 185HOUR 13 Graphical User Interfaces . 197HOUR 14 Character Controllers . 213HOUR 15 Game 3: Captain Blaster . 227HOUR 16 Particle Systems. 245HOUR 17 Animations . 261HOUR 18 Animators . 277HOUR 19 Game 4: Gauntlet Runner. 297HOUR 20 Audio . 317HOUR 21 Mobile Development . 329HOUR 22 Game Revisions. 341HOUR 23 Polish and Deploy. 353HOUR 24 Wrap Up . 367Index . 373

Table of ContentsPrefacexiHOUR 1: Introduction to Unity1Installing Unity. 1Getting to Know the Unity Editor. 5Navigating the Unity Scene View . 18Summary . 20Q&A . 20Workshop. 21Exercise . 21HOUR 2: Game Objects23Dimensions and Coordinate Systems . 23Game Objects . 27Transforms . 28Summary . 34Q&A . 34Workshop. 35Exercise . 35HOUR 3: Models, Materials, and Textures37The Basics of Models . 37Textures, Shaders, and Materials . 42Summary . 48Q&A . 48Workshop. 49Exercise . 49HOUR 4: Terrain51Terrain Generation . 51Terrain Textures . 59Summary . 62

vContentsQ&A . 62Workshop. 63Exercise . 63HOUR 5: Environments65Generating Trees and Grass . 65Environment Effects . 72Character Controllers . 77Summary . 79Q&A . 79Workshop. 79Exercise . 80HOUR 6: Lights and Cameras81Lights . 81Cameras . 90Layers . 94Summary . 98Q&A . 98Workshop. 98Exercise . 99HOUR 7: Game 1: Amazing Racer101Design . 101Creating the Game World . 104Gamification. 106Playtesting . 113Summary . 114Q&A . 114Workshop. 114Exercise . 115HOUR 8: Scripting Part 1117Scripts . 118Variables . 125Operators . 127Conditionals . 130

viSams Teach Yourself Unity Game Development in 24 HoursIteration . 133Summary . 135Q&A . 135Workshop. 136Exercise . 136HOUR 9: Scripting Part 2137Methods . 137Input . 142Accessing Local Components . 147Accessing Other Objects . 148Summary . 152Q&A . 152Workshop. 152Exercise . 153HOUR 10: Collision155Rigidbodies. 155Collision . 157Triggers . 161Raycasting . 163Summary . 165Q&A . 165Workshop. 165Exercise . 166HOUR 11: Game 2: Chaos Ball167Design . 167The Arena . 169Game Entities. 173The Control Objects . 178Improving the Game . 182Summary . 183Q&A . 183Workshop. 183Exercise . 184

viiContentsHOUR 12: Prefabs185Prefab Basics . 185Working with Prefabs . 188Instantiating Prefabs Through Code . 194Summary . 194Q&A . 195Workshop. 195Exercise . 195HOUR 13: Graphical User Interfaces197GUI Basics . 197GUI Controls . 199Customization . 205Summary . 211Q&A . 211Workshop. 211Exercise . 212HOUR 14: Character Controllers213The Character Controller . 213Scripting for Character Controllers . 216Building a Controller. 219Summary . 225Q&A . 225Workshop. 225Exercise . 226HOUR 15: Game 3: Captain Blaster227Design . 227The World . 229Controls. 234Improvements. 242Summary . 242Q&A . 242Workshop. 243Exercise . 244

viiiSams Teach Yourself Unity Game Development in 24 HoursHOUR 16: Particle Systems245Particle Systems . 245Particle System Modules . 247The Curve Editor . 257Summary . 259Q&A . 259Workshop. 259Exercise . 259HOUR 17: Animations261Animation Basics . 262Preparing a Model for Animation . 263Applying Animations . 269Scripting Animations . 272Summary . 273Q&A . 273Workshop. 274Exercise . 274HOUR 18: Animators277Animator Basics . 278Creating an Animator. 287Scripting Animators . 294Summary . 296Q&A . 296Workshop. 296Exercise . 296HOUR 19: Game 4: Gauntlet Runner297Design . 297The World . 298The Entities . 300The Controls . 307Room for Improvement . 314Summary . 315

ixContentsQ&A . 315Workshop. 315Exercise . 316HOUR 20: Audio317Audio Basics . 317Audio Sources . 319Audio Scripting . 324Summary . 326Q&A . 326Workshop. 327Exercise . 327HOUR 21: Mobile Development329Preparing for Mobile . 329Accelerometers . 333Summary . 338Q&A . 338Workshop. 338Exercise . 339HOUR 22: Game Revisions341Amazing Racer . 341Chaos Ball . 345Captain Blaster . 346Gauntlet Runner . 349Summary . 350Q&A . 351Workshop. 351Exercise . 351HOUR 23: Polish and Deploy353Managing Scenes. 353Persisting Data and Objects . 356Unity Player Settings . 359Building Your Game . 362

xSams Teach Yourself Unity Game Development in 24 HoursSummary . 365Q&A . 365Workshop. 365Exercise . 366HOUR 24: Wrap Up367Accomplishments . 367Where to Go from Here . 370Resources Available to You . 371Summary . 371Q&A . 371Workshop. 372Exercise . 372Index373

PrefaceThe Unity game engine is an incredibly powerful and popular choice for professional andamateur game developers alike. This book has been written to get readers up to speed andworking in Unity as fast as possible (about 24 hours to be exact) while covering fundamental principles of game development. Unlike other books that only cover specific topics orspend the entire time teaching a single game, this book covers a large array of topics whilestill managing to contain four games! Talk about a bargain. By the time you are done reading this book, you won’t have just theoretical knowledge of the Unity game engine. You willhave a portfolio of games to go with it.Who Should Read This BookThis book is for anyone looking to learn how to use the Unity game engine. Whether youare a student or a development expert, there is something to learn in these pages. It is notassumed that you have any prior game development knowledge or experience, so don’tworry if this is your first foray into the art of making games. Take your time and have fun.You will be learning in no time.How This Book Is Organized and WhatIt CoversFollowing the Sam’s Teach Yourself approach, this book is organized into 24 chaptersthat should take approximately 1 hour each to work through. The chapters include thefollowing: Hour 1, “Introduction to Unity”: This hour gets you up and running with the variouscomponents of the Unity game engine. Hour 2, “Game Objects”: Hour 2 teaches you how to use the fundamental buildingblocks of the Unity game engine: the game object. You also learn about coordinatesystems and transformations. Hour 3, “Models, Materials, and Textures”: In this hour, you learn to work with Unity’sgraphical asset pipeline as you apply shaders and textures to materials. You also learnhow to apply those materials to a variety of 3D objects. Hour 4, “Terrain”: In Hour 4, you learn to sculpt game worlds using Unity’s terrainsystem. Don’t be afraid to get your hands dirty as you dig around and create uniqueand stunning landscapes.

xiiSams Teach Yourself Unity Game Development in 24 Hours Hour 5, “Environments”: In this hour, you learn to apply environmental effects to yoursculpted terrain. Time to plant some trees! Hour 6, “Lights and Cameras”: Hour 6 covers lights and cameras in great detail. Hour 7, “Game 1: Amazing Racer”: Time for your first game. In Hour 7, you createAmazing Racer, which requires you to take

The Unity game engine is an incredibly powerful and popular choice for professional and amateur game developers alike. This book has been written to get readers up to speed and working in Unity as fast as possible (about 24 hours to be exact) wh