3D Game Development with Unity 3D Game Development with Unity Franz Lanzinger First edition published 2022 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN © 2022 Franz Lanzinger CRC Press is an imprint of Taylor & Francis Group, LLC Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and p ublishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact [email protected] Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑in‑Publication Data Names: Lanzinger, Franz, author. Title: 3D game development with Unity / Franz Lanzinger. Other titles: Three D game development with Unity Description: First edition. | Boca Raton: CRC Press, 2022. | Includes bibliographical references and index. | Summary: “This book teachesbeginners and aspiring game developers how to develop 3D games with Unity. Thousands of commercial games have been built with Unity. Blender, the top open source 3D modeling and animation package, is also introduced”—Provided by publisher. Identifiers: LCCN 2021045810 | ISBN 9780367349219 (hbk) | ISBN 9780367349189 (pbk) |ISBN 9780429328725 (ebk) Subjects: LCSH: Computer games—Programming. | Three-dimensional display systems. | Unity (Electronic resource) Classification: LCC QA76.76.C672 L36338 2022 | DDC 794.8/1526—dc23 LC record available at https://lccn.loc.gov/2021045810 ISBN: 9780367349219 (hbk) ISBN: 9780367349189 (pbk) ISBN: 9780429328725 (ebk) DOI: 10.1201/9780429328725 Typeset in Minion by codeMantra Contents Acknowledgments, xi Author, xiii Introduction and Overview, xv Part I Basics of 3D Game Development ChaPter 1 ◾ Software Tools 3 YOUR COMPUTERS 3 NOTES FOR MAC USERS 5 VISUAL STUDIO 5 TYPOGRAPHIC CONVENTIONS 6 HELLO WORLD! 7 C# 8 UNITY 9 GIMP 10 BLENDER 14 AUDACITY 22 ChaPter 2 ◾ A 3D Game 25 PROJECT SETUP IN UNITY 25 IMPORTING THE TOY CAR 28 MAKING THE WORLD IN BLENDER 29 MAKING TEXTURES IN GIMP 34 MATERIALS IN UNITY 36 IMPROVING THE CAR 48 GAMEPLAY 56 CREATING BUILDINGS WITH THE ARRAY MODIFIER 63 SOUND 70 v vi ◾ Contents ChaPter 3 ◾ 3D 73 3D COORDINATES 73 3D IN BLENDER 75 3D IN UNITY 80 CAMERA VIEWS 82 3D ASSETS 86 ChaPter 4 ◾ Designing a 3D Remake 89 THE 2D GAME: DOTGAME 89 REMAKING DOTGAME 91 CONTROLS 92 THE CAMERA 92 ChaPter 5 ◾ 3D Dottima Character 93 SETTING UP THE PROJECT 93 MODELING 3D DOTTIMA IN BLENDER 94 TEXTURE PAINTING DOTTIMA 101 CONTROLLING DOTTIMA IN UNITY 107 ChaPter 6 ◾ Blender Modeling and Animation 111 REMAKING DOTROBOT FOR 3D 111 TEXTURING DOTROBOT 112 IMPORTING TO UNITY, PART 1 116 EXPERIMENTAL RIG 118 RIGGING DOTROBOT 127 REANIMATING DOTROBOT 133 IMPORTING TO UNITY, PART 2 138 ChaPter 7 ◾ More Art Assets 141 THE UNITY ASSET STORE 141 MILLIONS OF MESHES ON THE WEB 145 FREE TEXTURES 146 3D M ODELS FROM THE 2D GAME 146 3D P LAYFIELD WITH COLLISIONS 147 Contents ◾ vii ChaPter 8 ◾ First Playable 163 MOVING THE CAMERA 163 THREE LEVELS 165 3D S PIKERS 167 3D ARROWS FOR DOTTIMA 171 ARROWS IN UNITY: DOTTIMA INSTANTIATES ARROWS 176 ARROWS IN UNITY: ARROW MOVEMENT AND DIRECTION 180 ARROWS IN UNITY: COLLISION DETECTION FOR ARROWS 183 3D BLOCKADE 187 ChaPter 9 ◾ Game Structure 191 GAME STATE 191 GAME OVER 195 ChaPter 10 ◾ More Game Objects and a Large Level 199 DOTROBOT 199 BOMBS 204 A LARGE LEVEL 213 ChaPter 11 ◾ Sound and Music 217 REVIEWING SOUND AND MUSIC FROM THE 2D GAME 217 ANOTHER SOUND EFFECT 220 MORE MUSIC WITH MUSESCORE 222 3D AUDIO 224 ChaPter 12 ◾ GUI and Cutscenes 227 THE TITLE SCREEN 227 MENUS 230 SCORING 233 HIDING THE MOUSE CURSOR 234 ENDING CUTSCENE AND TIMELINE 235 CINEMACHINE 242 INTEGRATING THE ENDING CUTSCENE 248 viii ◾ Contents ChaPter 13 ◾ Testing and Debugging 251 TESTING 251 ANCIENT DEBUGGING TECHNIQUES 256 DEBUGGING C# IN UNITY 259 THE VISUAL STUDIO DEBUGGER 261 ChaPter 14 ◾ Input 265 UNITY INPUT SYSTEMS 265 GAMEPAD SUPPORT IN DOTGAME3D 267 ChaPter 15 ◾ Preparing DotGame3D for Release 269 TESTING FOR RELEASE: BUILD AND RUN 269 TESTING ON DIFFERENT PLATFORMS 270 THE UNITY PROFILER 271 RELEASE AND POSTMORTEM 272 Part II A 3D Adventure ChaPter 16 ◾ FPS, Etc. 277 A BRIEF HISTORY OF FPS GAMES 277 DESIGNING AN FPS ADVENTURE 278 ChaPter 17 ◾ Worldbuilding 281 USING BLENDER FOR TERRAIN GENERATION 281 BLENDER LANDSCAPE IN UNITY 288 USING UNITY FOR TERRAIN GENERATION 290 SKYBOXES 302 ChaPter 18 ◾ Character Controller 305 IMPORTING A CHARACTER FROM THE ASSET STORE 305 CHARACTER MOVEMENT 308 IDLE, WALK, AND RUN ANIMATIONS 312 GRAVITY 316 ChaPter 19 ◾ First Playable 321 3D G AME KIT 321 ENEMIES 326 ENEMY MOVEMENT 330 Contents ◾ ix MEDIEVAL TOWN 335 NAVMESH AND ENEMY AI 337 CREATING A LARGE LEVEL 339 GUI AND SCORING 342 ENEMY COLLISIONS 344 ChaPter 20 ◾ A 3D Puzzle Room 347 PUZZLE DESIGN RULES 347 PUZZLE ROOM GRAPHICS 348 BUILDING THE PUZZLE ROOM 351 ChaPter 21 ◾ Shaders and Lighting in Unity 355 INTRODUCTION 355 SHADERS IN UNITY 356 DIRECT LIGHTING 358 INDIRECT LIGHTING 363 RAYTRACING 366 ChaPter 22 ◾ Physics 371 UNITY PHYSICS OVERVIEW 371 A PHYSICS PUZZLE 374 CLOTH 379 RAGDOLL WIZARD 381 ChaPter 23 ◾ Sound and Music, Part 2 385 SOUND PACKAGES FROM THE UNITY ASSET STORE 385 ChaPter 24 ◾ Releasing FPSAdventure? 387 STEAM EARLY ACCESS? 387 TESTING AND DEBUGGING 388 BUILD AND RUN 388 POSTMORTEM 388 ChaPter 25 ◾ Epilogue 389 LEARNINGS 389 WHAT NEXT? 389 PROJECT 1: CARD GAME 389 PROJECT 2: R ACING GAME 390