Unity in Action, Third Edition

Book description

This hands-on beginners guide gets you building games fast, all with the awesome Unity engine! You’ll speed past the basics and use your existing coding skills to create 2D, 3D, and AR/VR games.

In Unity in Action, Third Edition, you will learn how to:
  • Create characters that run, jump, and bump into things
  • Build 3D first-person shooters and third-person action games
  • Construct 2D card games and side-scrolling platformers
  • Script enemies with AI
  • Improve game graphics by importing models and images
  • Design an intuitive user interface for your games
  • Play music and spatially-aware sound effects
  • Connect your games to the internet for online play
  • Deploy your games to desktop, mobile, and the web

Thousands of new game developers have chosen Joe Hocking’s Unity in Action as their first step toward Unity mastery. Starting with the initial groundwork of a new game development project, you’ll quickly start writing custom code instead of clicking together premade scripts. This fully updated third edition comes packed with fully refreshed graphics, Unity’s latest features, and coverage of augmented and virtual reality toolkits. You'll master the Unity toolset from the ground up, learning the skills to go from application coder to game developer.

About the Technology
Writing games is rewarding and fun—and with Unity, it’s easy to get started! Unity handles the heavy lifting, so you can focus on game play, graphics, and user experience. C# support and a huge ecosystem of prebuilt components help even first-time developers go from the drawing board to the screen fast.

About the Book
Unity in Action, Third Edition teaches you to create games with the Unity game platform. It’s many 2D, 3D, and AR/VR game examples give you hands-on experience with Unity’s workflow tools and state-of-the-art rendering engine. This fully updated third edition presents new coverage of Unity’s XR toolkit and shows you how you can start building with virtual and augmented reality.

What's Inside
  • Create characters that run, jump, and bump into things
  • Script enemies with AI
  • Play music and spatially-aware sound effects
  • Deploy your games to desktop, mobile, and the web


About the Reader
For programmers who know any object-oriented programming language. Examples are in C#.

About the Author
Joe Hocking is a software engineer who specializes in interactive media development. He currently works for Qualcomm, wrote most of the third edition while working for BUNDLAR, and wrote the first edition while at Synapse Games. He has also taught classes at the University of Illinois Chicago, the School of the Art Institute of Chicago, and Columbia College Chicago. He lives in the Chicago suburbs with his wife and two kids. His website is www.newarteest.com.

Quotes
What I love most about Unity in Action is the ‘action’ part. Joe Hocking wastes none of your time and gets you coding fast!
- From the Foreword by Jesse Schell, CEO of Shell Games

The book you should buy if you want to get a thorough understanding of how to use Unity. Highly recommended!
- Nick Keers, cinch

A practical and didactic book that will take you by the hand and teach you how to create amazing games.
- Alain Lompo, ISO-Gruppe

An all-around comprehensive resource to get you started with Unity.
- Krishna Chaitanya Anipindi, Hexagon

Publisher resources

View/Submit Errata

Table of contents

  1. inside front cover
  2. Praise for earlier editions of Unity in Action
  3. Unity in Action, Third Edition
  4. Copyright
  5. contents
  6. front matter
    1. foreword
    2. preface
    3. acknowledgments
    4. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    5. about the author
    6. about the cover illustration
  7. Part 1 First steps
  8. 1 Getting to know Unity
    1. 1.1 Why is Unity so great?
      1. 1.1.1 Unity’s strengths and advantages
      2. 1.1.2 Downsides to be aware of
      3. 1.1.3 Example games built with Unity
    2. 1.2 How to use Unity
      1. 1.2.1 Scene view, Game view, and the Toolbar
      2. 1.2.2 The mouse and keyboard
      3. 1.2.3 The Hierarchy view and the Inspector panel
      4. 1.2.4 The Project and Console tabs
    3. 1.3 Getting up and running with Unity programming
      1. 1.3.1 Running code in Unity: Script components
      2. 1.3.2 Using Visual Studio, the included IDE
      3. 1.3.3 Printing to the console: Hello World!
      4. Summary
  9. 2 Building a demo that puts you in 3D space
    1. 2.1 Before you start . . .
      1. 2.1.1 Planning the project
      2. 2.1.2 Understanding 3D coordinate space
    2. 2.2 Begin the project: Place objects in the scene
      1. 2.2.1 The scenery: Floor, outer walls, and inner walls
      2. 2.2.2 Lights and cameras
      3. 2.2.3 The player’s collider and viewpoint
    3. 2.3 Make things move: A script that applies transforms
      1. 2.3.1 Visualizing how movement is programmed
      2. 2.3.2 Writing code to implement the diagram
      3. 2.3.3 Understanding local vs. global coordinate space
    4. 2.4 Script component for looking around: MouseLook
      1. 2.4.1 Horizontal rotation that tracks mouse movement
      2. 2.4.2 Vertical rotation with limits
      3. 2.4.3 Horizontal and vertical rotation at the same time
    5. 2.5 Keyboard input component: First-person controls
      1. 2.5.1 Responding to keypresses
      2. 2.5.2 Setting a rate of movement independent of the computer’s speed
      3. 2.5.3 Moving the CharacterController for collision detection
      4. 2.5.4 Adjusting components for walking instead of flying
    6. Summary
  10. 3 Adding enemies and projectiles to the 3D game
    1. 3.1 Shooting via raycasts
      1. 3.1.1 What is raycasting?
      2. 3.1.2 Using the ScreenPointToRay command for shooting
      3. 3.1.3 Adding visual indicators for aiming and hits
    2. 3.2 Scripting reactive targets
      1. 3.2.1 Determining what was hit
      2. 3.2.2 Alerting the target that it was hit
    3. 3.3 Basic wandering AI
      1. 3.3.1 Diagramming how basic AI works
      2. 3.3.2 “Seeing” obstacles with a raycast
      3. 3.3.3 Tracking the character’s state
    4. 3.4 Spawning enemy prefabs
      1. 3.4.1 What is a prefab?
      2. 3.4.2 Creating the enemy prefab
      3. 3.4.3 Instantiating from an invisible SceneController
    5. 3.5 Shooting by instantiating objects
      1. 3.5.1 Creating the projectile prefab
      2. 3.5.2 Shooting the projectile and colliding with a target
      3. 3.5.3 Damaging the player
    6. Summary
  11. 4 Developing graphics for your game
    1. 4.1 Understanding art assets
    2. 4.2 Building basic 3D scenery: Whiteboxing
      1. 4.2.1 Whiteboxing explained
      2. 4.2.2 Drawing a floor plan for the level
      3. 4.2.3 Laying out primitives according to the plan
    3. 4.3 Texturing the scene with 2D images
      1. 4.3.1 Choosing a file format
      2. 4.3.2 Importing an image file
      3. 4.3.3 Applying the image
    4. 4.4 Generating sky visuals by using texture images
      1. 4.4.1 What is a skybox?
      2. 4.4.2 Creating a new skybox material
    5. 4.5 Working with custom 3D models
      1. 4.5.1 Which file format to choose?
      2. 4.5.2 Exporting and importing the model
    6. 4.6 Creating effects by using particle systems
      1. 4.6.1 Adjusting parameters on the default effect
      2. 4.6.2 Applying a new texture for fire
      3. 4.6.3 Attaching particle effects to 3D objects
    7. Summary
  12. Part 2 Getting comfortable
  13. 5 Building a Memory game using Unity’s 2D functionality
    1. 5.1 Setting up everything for 2D graphics
      1. 5.1.1 Preparing the project
      2. 5.1.2 Displaying 2D images (aka sprites)
      3. 5.1.3 Switching the camera to 2D mode
    2. 5.2 Building a card object and making it react to clicks
      1. 5.2.1 Building the object out of sprites
      2. 5.2.2 Mouse input code
      3. 5.2.3 Revealing the card on a click
    3. 5.3 Displaying the various card images
      1. 5.3.1 Loading images programmatically
      2. 5.3.2 Setting the image from an invisible SceneController
      3. 5.3.3 Instantiating a grid of cards
      4. 5.3.4 Shuffling the cards
    4. 5.4 Making and scoring matches
      1. 5.4.1 Storing and comparing revealed cards
      2. 5.4.2 Hiding mismatched cards
      3. 5.4.3 Text display for the score
    5. 5.5 Restart button
      1. 5.5.1 Programming a UIButton component by using SendMessage
      2. 5.5.2 Calling LoadScene from SceneController
      3. Summary
  14. 6 Creating a basic 2D platformer
    1. 6.1 Setting up the graphics
      1. 6.1.1 Placing the scenery
      2. 6.1.2 Importing sprite sheets
    2. 6.2 Moving the player left and right
      1. 6.2.1 Writing keyboard controls
      2. 6.2.2 Colliding with the block
    3. 6.3 Playing the sprite’s animation
      1. 6.3.1 Explaining the Mecanim animation system
      2. 6.3.2 Triggering animations from code
    4. 6.4 Adding the ability to jump
      1. 6.4.1 Falling from gravity
      2. 6.4.2 Applying an upward impulse
      3. 6.4.3 Detecting the ground
    5. 6.5 Additional features for a platform game
      1. 6.5.1 Unusual floors: Slopes and one-way platforms
      2. 6.5.2 Implementing moving platforms
      3. 6.5.3 Camera control
    6. Summary
  15. 7 Putting a GUI onto a game
    1. 7.1 Before you start writing code . . .
      1. 7.1.1 Immediate mode GUI or advanced 2D interface?
      2. 7.1.2 Planning the layout
      3. 7.1.3 Importing UI images
    2. 7.2 Setting up the GUI display
      1. 7.2.1 Creating a canvas for the interface
      2. 7.2.2 Buttons, images, and text labels
      3. 7.2.3 Controlling the position of UI elements
    3. 7.3 Programming interactivity in the UI
      1. 7.3.1 Programming an invisible UIController
      2. 7.3.2 Creating a pop-up window
      3. 7.3.3 Setting values using sliders and input fields
    4. 7.4 Updating the game by responding to events
      1. 7.4.1 Integrating an event system
      2. 7.4.2 Broadcasting and listening for events from the scene
      3. 7.4.3 Broadcasting and listening for events from the HUD
    5. Summary
  16. 8 Creating a third-person 3D game: Player movement and animation
    1. 8.1 Adjusting the camera view for third-person
      1. 8.1.1 Importing a character to look at
      2. 8.1.2 Adding shadows to the scene
      3. 8.1.3 Orbiting the camera around the player character
    2. 8.2 Programming camera-relative movement controls
      1. 8.2.1 Rotating the character to face movement direction
      2. 8.2.2 Moving forward in that direction
    3. 8.3 Implementing the jump action
      1. 8.3.1 Applying vertical speed and acceleration
      2. 8.3.2 Modifying the ground detection to handle edges and slopes
    4. 8.4 Setting up animations on the player character
      1. 8.4.1 Defining animation clips in the imported model
      2. 8.4.2 Creating the animator controller for these animations
      3. 8.4.3 Writing code that operates the animator
    5. Summary
  17. 9 Adding interactive devices and items within the game
    1. 9.1 Creating doors and other devices
      1. 9.1.1 Doors that open and close on a keypress
      2. 9.1.2 Checking distance and facing before opening the door
      3. 9.1.3 Operating a color-changing monitor
    2. 9.2 Interacting with objects by bumping into them
      1. 9.2.1 Colliding with physics-enabled obstacles
      2. 9.2.2 Operating the door with a trigger object
      3. 9.2.3 Collecting items scattered around the level
    3. 9.3 Managing inventory data and game state
      1. 9.3.1 Setting up player and inventory managers
      2. 9.3.2 Programming the game managers
      3. 9.3.3 Storing inventory in a collection object: List vs. Dictionary
    4. 9.4 Inventory UI for using and equipping items
      1. 9.4.1 Displaying inventory items in the UI
      2. 9.4.2 Equipping a key to use on locked doors
      3. 9.4.3 Restoring the player’s health by consuming health packs
    5. Summary
  18. Part 3 Strong finish
  19. 10 Connecting your game to the internet
    1. 10.1 Creating an outdoor scene
      1. 10.1.1 Generating sky visuals by using a skybox
      2. 10.1.2 Setting up an atmosphere that’s controlled by code
    2. 10.2 Downloading weather data from an internet service
      1. 10.2.1 Requesting HTTP data using coroutines
      2. 10.2.2 Parsing XML
      3. 10.2.3 Parsing JSON
      4. 10.2.4 Affecting the scene based on weather data
    3. 10.3 Adding a networked billboard
      1. 10.3.1 Loading images from the internet
      2. 10.3.2 Displaying images on the billboard
      3. 10.3.3 Caching the downloaded image for reuse
    4. 10.4 Posting data to a web server
      1. 10.4.1 Tracking current weather: Sending post requests
      2. 10.4.2 Server-side code in PHP
      3. Summary
  20. 11 Playing audio: Sound effects and music
    1. 11.1 Importing sound effects
      1. 11.1.1 Supported file formats
      2. 11.1.2 Importing audio files
    2. 11.2 Playing sound effects
      1. 11.2.1 Explaining what’s involved: Audio clip vs. source vs. listener
      2. 11.2.2 Assigning a looping sound
      3. 11.2.3 Triggering sound effects from code
    3. 11.3 Using the audio control interface
      1. 11.3.1 Setting up the central AudioManager
      2. 11.3.2 Volume control UI
      3. 11.3.3 Playing UI sounds
    4. 11.4 Adding background music
      1. 11.4.1 Playing music loops
      2. 11.4.2 Controlling music volume separately
      3. 11.4.3 Fading between songs
    5. Summary
  21. 12 Putting the parts together into a complete game
    1. 12.1 Building an action RPG by repurposing projects
      1. 12.1.1 Assembling assets and code from multiple projects
      2. 12.1.2 Programming point-and-click controls: Movement and devices
      3. 12.1.3 Replacing the old GUI with a new interface
    2. 12.2 Developing the overarching game structure
      1. 12.2.1 Controlling mission flow and multiple levels
      2. 12.2.2 Completing a level by reaching the exit
      3. 12.2.3 Losing the level when caught by enemies
    3. 12.3 Handling the player’s progression through the game
      1. 12.3.1 Saving and loading the player’s progress
      2. 12.3.2 Beating the game by completing three levels
    4. Summary
  22. 13 Deploying your game to players’ devices
    1. 13.1 Start by building for the desktop: Windows, Mac, and Linux
      1. 13.1.1 Building the application
      2. 13.1.2 Adjusting player settings: Setting the game’s name and icon
      3. 13.1.3 Platform-dependent compilation
    2. 13.2 Building for the web
      1. 13.2.1 Building the game embedded in a web page
      2. 13.2.2 Communicating with JavaScript in the browser
    3. 13.3 Building for mobile: iOS and Android
      1. 13.3.1 Setting up the build tools
      2. 13.3.2 Texture compression
      3. 13.3.3 Developing plugins
    4. 13.4 Developing XR (both VR and AR)
      1. 13.4.1 Supporting virtual reality headsets
      2. 13.4.2 AR Foundation for mobile Augmented Reality
    5. Summary
  23. afterword
    1. Game design
    2. Marketing your game
  24. Appendix A. Scene navigation and keyboard shortcuts
    1. A.1 Scene navigation using the mouse
    2. A.2 Commonly used keyboard shortcuts
  25. Appendix B. External tools used alongside Unity
    1. B.1 Programming tools
      1. B.1.1 Rider
      2. B.1.2 Xcode
      3. B.1.3 Android SDK
      4. B.1.4 Version control (Git, SVN)
    2. B.2 3D art applications
      1. B.2.1 Maya
      2. B.2.2 3ds Max
      3. B.2.3 Blender
      4. B.2.4 SketchUp
    3. B.3 2D image editors
      1. B.3.1 Photoshop
      2. B.3.2 GIMP
      3. B.3.3 TexturePacker
      4. B.3.4 Aseprite, Pyxel Edit
    4. B.4 Audio software
      1. B.4.1 Pro Tools
      2. B.4.2 Audacity
  26. Appendix C. Modeling a bench in Blender
    1. C.1 Building the mesh geometry
    2. C.2 Texture-mapping the model
  27. Appendix D. Online learning resources
    1. D.1 Additional tutorials
    2. D.2 Code libraries
  28. index
  29. inside back cover

Product information

  • Title: Unity in Action, Third Edition
  • Author(s): Joseph Hocking
  • Release date: January 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617299339