Preface

Welcome to the Unity Game Development Cookbook! We’re incredibly excited about this book, and really proud of our work on it. We hope you find it a useful companion to your game development using the Unity engine.

Unity is an amazing platform, and the more you use it the more you’ll come up with new ideas on how you can use it, why you could use it, and so on! It’s all very exciting. Trust us, we’ve been there.

As Secret Lab, we use Unity to build most of our video games. We’re best known for building children’s games in Australia, as well as our contributions to the BAFTA- and IGF-winning Night in the Woods. We discovered Unity more than a decade ago, and we haven’t stopped using it since.

In this book, we’ve assembled a selection of recipes to solve common problems that we found ourselves solving over and over as we build video games using Unity. This book won’t cover every single tiny thing you want to know about Unity, but it hopefully covers the majority of things you’d be doing day-to-day in video game development.

These recipes are about things we wish we had someone to ask about back when we were first building our own games. We really hope they help you!

It’s a really exciting time to be using Unity. The game development world is getting bigger and more interesting every day, and the tools and technologies we use are better than ever.

Go out and build great games!

Tip

If you want to get more resources for this book, and our other books, check out our website.

Resources Used in This Book

If you want to download all our code from the recipes throughout this book, head to https://github.com/thesecretlab/unity-game-development-cookbook-1e. We also have a dedicated page for this book, where we keep both the source code as well as other downloads and resources.

Audience and Approach

This book assumes that you know a little of the C# programming language, or a similar language like C++, Java, Go, Swift, Python, or other common languages, but don’t know anything about Unity. This book is intended to be a companion to get your games up and running faster.

Tip

If you like our style, and want to read more Unity material from us, you can learn Unity from scratch in Mobile Game Development with Unity, also available from the fine folks at O’Reilly.

Our screenshots are taken with macOS, but everything we talk about is generally applicable to Unity on Windows, macOS, or Linux.

Organization of This Book

This book is arranged into 12 chapters:

  • Chapter 1 introduces the fundamental concepts you need to know in order to use Unity: game objects, components, scenes, and how to work with the Unity editor itself.

  • Chapter 2 covers the scripting interface of Unity, which is the way you write the actual code that defines the way your game works. After we establish the basics, we’ll dive into some practical examples, including how to write a save and load system, how to efficiently work with objects, and how to store your data in a way that both your code and Unity can work with easily.

  • In Chapter 3, you’ll learn how to get input from your user, in the form of keyboard input, the mouse, and from game pads. We’ll also discuss how to set up game objects that react to input events, like the user clicking on them.

  • Chapter 4 covers the fundamentals of some of the mathematical concepts that are useful to know about when development games, such as vectors, matrices and quaternions. We’ll also look at some practical uses of these concepts, like detecting if an object is in front of the player or not.

  • Chapter 5 discusses the 2D graphics and physics systems that are built in to Unity. You’ll learn how to display sprites, sort them, and make them bounce off each other.

  • Chapter 6 covers the material and shading system of Unity: how materials and shaders work, how to build a shader in Unity, and how to get the best results in your scenes.

  • In Chapter 7, you’ll learn how to make the 3D physics system of Unity do what you need it to in order to support common gameplay tasks, like picking up and throwing objects, and creating a moving platform that the player can ride on.

  • Chapter 8 introduces the animation system used in Unity, and covers topics like how to set up a character to blend between different animation states, and how to integrate player movement with character animation. We’ll also introduce the camera system in Unity, and discuss how to set up cameras that move to follow targets.

  • Chapter 9 is all about creating the gameplay that players interact with. There’s a wide range of common gameplay tasks in here, like managing the state of a quest that you’ve given the player, tracking if a racing car is taking too much of a shortcut, and managing how damage gets dealt between objects in your game.

  • In Chapter 10, you’ll learn how to add brains to your characters, including the ability to detect when the player can be seen, the ability to navigate a space and avoid obstacles, and the ability to figure out a place where they can’t be seen.

  • Chapter 11 introduces the audio systems in Unity. We’ll cover the basics of playing sounds, and then move on to more advanced features, like routing audio to multiple groups, and automatically ducking the music when a character is speaking.

  • Chapter 12, our final chapter, covers the tools for building an interface for your players to look at and interact with. We’ll cap the whole book off with a section on how to build your own tools in Unity, by extending the editor.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, errata, etc.) is available for download from our website.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Unity Game Development Cookbook by Paris Buttfield-Addison, Jon Manning, and Tim Nugent (O’Reilly). Copyright 2019 Secret Lab, 978-1-491-99915-8.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Online Learning Platform

Note

For almost 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, conferences, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, please visit http://oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/unity-game-dev-cb.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Jon thanks his mother, father, and the rest of his crazily extended family for their tremendous support.

Paris thanks his amazing mother, without whom he wouldn’t be doing much of anything at all really, let alone with enthuasiam. She’s about to submit her doctorate for assessment, and he’s sure she will be absolutely amazing at it.

Tim thanks his parents and family for putting up with his rather lackluster approach to life.

We’d all like to thank Rachel Roumeliotis, whose skill and advice were invaluable in completing this book, as well as every other book we’ve written together. We thoroughly look forward to our coffee catchups every few months/year or so!

Thank you to our editor on this book, Jeff Bleiel. You were calm, clear, enthusiastic, and wonderful to work with. We hope to work on more books with you in the future!

Likewise, all the O’Reilly Media staff we’ve interacted with over the course of writing the book have been the absolute gurus of their fields. O’Reilly Media has a vast array of incredibly talented people on staff, on contract, and generally surrounding them. It’s actually terrifying how good they are at their jobs.

A huge thank you to Tony Gray and the Apple University Consortium (AUC) for the monumental boost they gave us and others listed on this page. We wouldn’t be writing this book if it weren’t for them. And now you’re writing books, too, Tony—sorry about that!

Thanks also to Neal Goldstein, who deserves full credit and/or blame for getting us into the whole book-writing racket. We’re thankful for the support of the goons at MacLab (who know who they are and continue to stand watch for Admiral Dolphin’s inevitable apotheosis), as well as Professor Christopher Lueg, Associate Professor Leonie Ellis, and the staff at the University of Tasmania for putting up with us.

Thanks also to Mars G. for generally being amazing, as well as Dave J. for serving the coffee. If you ever visit Hobart, Tasmania, Australia, make sure you get a coffee at Yellow Bernard. It’s the best.

Thanks also to our tech reviewers for their thoroughness and professionalism. The reviews were brilliantly helpful.

Finally, thank you very much for buying our book—we appreciate it! And if you have any feedback, please let us know. You can email us at lab@secretlab.com.au and find us on Twitter at @thesecretlab.

Get Unity Game Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.