Complete Unity 2018 Game Development - Alan Thorn - E-Book

Complete Unity 2018 Game Development E-Book

Alan Thorn

0,0
41,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Leverage the power of the Unity 2018 game engine to create games with brilliant gameplay and high replayability.

Key Features

  • Develop different types of games from scratch with Unity 2018
  • Discover the secrets of creating AAA quality shaders without writing long algorithms
  • Script intelligent game characters and agents using Artificial Intelligence techniques and algorithms

Book Description

Through this Learning Path, you’ll learn how to leverage the features of Unity and create amazing games, ranging from action shooters and mind-bending puzzle games to adventure and Virtual Reality(VR) games. If you have no prior experience of using Unity, you can still benefit from this Learning Path, which easily explains the complete working of the Unity toolset.

You’ll start by learning how to create compelling shaders using Unity and understanding everything you need to know about vectors. This includes useful inputs on how lighting is constructed with vectors and how textures are used to create complex effects without the need for advanced math. In the succeeding chapters, you’ll also be able to use popular AI techniques, such as A* and A*mbush to develop intelligent pathfinding agents for your games. The book will also guide you through different algorithms for creating decision-making agents that go beyond simple behaviors and movement.

By the end of this Learning Path, you will have developed all the basic skills to create highly engaging and replayable games.

This Learning Path includes content from the following Packt products:

  • Unity 2018 By Example - Second Edition by Alan Thorn
  • Unity 2018 Shaders and Effects Cookbook - Third Edition by John P. Doran, Alan Zucconi
  • Unity 2018 Artificial Intelligence Cookbook - Second Edition by Jorge Palacios

What you will learn

  • Understand concepts such as game objects, components, and scenes
  • Create functional games with C# scripting
  • Write shaders from scratch in ShaderLab and HLSL/Cg
  • Develop intelligent pathfinding agents with A and Ambush
  • Work with terrains and world-creation tools
  • Simulate senses for agents to make decisions based on the environment
  • Implement waypoints by creating a manual selector
  • Enhance games with volumetric explosions, special effects, and visuals

Who this book is for

If you are a game developer who wants to learn tools that can transform your gameplay, this beginner-level Learning Path is ideal for you. Having basic knowledge of C# will help you grasp the concepts explained in the book easily.

Alan Thorn is an educator, author, and multidisciplinary games-developer with 18 years of tech-industry experience. He is currently Head of Department for the Games Design and Development MA degree at the BAFTA-winning National Film and Television School, London. He has written 27 technical books on game development and presented 29 online training courses. He founded the game-studio Wax Lyrical Games and created the award-winning game Baron Wittard: Nemesis of Ragnarok. John P. Doran is a passionate and seasoned technical game designer, software engineer, and author who is based in Redmond, Washington. For over a decade, John has built games in roles ranging from game designer to lead UI programmer. Additionally, John has taught game development in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. Alan Zucconi is a passionate developer, author, and lecturer, recognized as one of Develop's 30 under 30. His expertise has been built over the past 10 years, working in the intersection between creativity and education. Starting in 2015, he published a series of educational tutorials for other developers and machine learning enthusiasts. Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 596

Veröffentlichungsjahr: 2019

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Complete Unity 2018 Game Development

 

 

 

 

 

 

 

Explore techniques to build 2D/3D applications using
real-world examples

 

 

 

 

 

 

 

Alan Thorn
John P. Doran
Alan Zucconi Jorge Palacios

 

 

 

 

 

BIRMINGHAM - MUMBAI

Complete Unity 2018 Game Development

 

Copyright © 2019 Packt Publishing

 

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

First published: January 2019

 

Production reference: 1310119

 

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

 

ISBN 978-1-78995-286-5

 

www.packtpub.com

 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

Packt.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

Contributors

About the authors

Alan Thorn is an educator, author, and multidisciplinary games-developer with 18 years of tech-industry experience. He is currently Head of Department for the Games Design and Development MA degree at the BAFTA-winning National Film and Television School, London. He has written 27 technical books on game development and presented 29 online training courses. He founded the game-studio Wax Lyrical Games and created the award-winning game Baron Wittard: Nemesis of Ragnarok.

 

John P. Doran is a passionate and seasoned technical game designer, software engineer, and author who is based in Redmond, Washington. For over a decade, John has built games in roles ranging from game designer to lead UI programmer. Additionally, John has taught game development in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development.

Thanks so much to my wife Hien and my family for all of their support while working on this book.

Alan Zucconi is a passionate developer, author, and lecturer, recognized as one of Develop's 30 under 30. His expertise has been built over the past 10 years, working in the intersection between creativity and education. Starting in 2015, he published a series of educational tutorials for other developers and machine learning enthusiasts.

 

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5.

He's also a game-programming instructor, speaker, and game-jam organizer.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright

Complete Unity 2018 Game Development

About Packt

Why subscribe?

Packt.com

Contributors

About the authors

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Conventions used

Get in touch

Reviews

Unity Fundamentals

Game design

Getting started – Unity and projects

Projects and project folders

Importing assets

Starting a level

Transformations and navigation

Scene building

Lighting and sky

Play testing and the Game tab

Adding a water plane

Adding a coin to collect

Summary

Creating a Collection Game

Creating a coin material

C# scripting in Unity

Counting coins

Code Sample 2.3

Collecting coins

Code Sample 2.5

Coins and prefabs

Timers and countdowns

Code Sample 2.6

Celebrations and fireworks!

Code Sample 2.7

Play testing

Building

Post Processing Stack

Introduction

Installing the Post Processing Stack

Getting ready

How to do it...

Getting a filmic look using grain, vignetting, and depth of field

Getting ready

How to do it...

How it works...

Mimicking real life with bloom and anti-aliasing

Getting ready

How to do it...

How it works...

Setting mood with color grading

Getting ready

How to do it...

Creating a horror game look with fog

Getting ready

How to do it...

How it works...

Creating Your First Shader

Introduction

Creating a basic Standard Shader

Getting ready

How to do it...

How it works...

There's more...

Adding properties to a shader

Getting ready

How to do it...

How it works...

See also

Using properties in a Surface Shader

How to do it...

How it works...

There's more...

See also

Surface Shaders and Texture Mapping

Introduction

Diffuse shading

Getting ready

How to do it...

How it works...

Accessing and modifying packed arrays

How to do it...

There's more...

See also

Adding a texture to a shader

Getting ready

How to do it...

How it works...

There's more...

See also

Scrolling textures by modifying UV values

Getting ready

How to do it...

How it works...

Creating a shader with normal mapping

Getting ready

How to do it...

How it works...

There's more...

Creating a transparent material

Getting ready

How to do it...

How it works...

Creating a Holographic Shader

Getting ready

How to do it...

How it works...

There's more...

See also

Packing and blending textures

Getting ready

How to do it...

How it works...

Creating a circle around your terrain

Getting ready

How to do it...

Moving the circle

How it works...

Understanding Lighting Models

Introduction

Creating a custom diffuse lighting model

Getting ready

How to do it...

How it works...

Creating a toon shader

Getting ready

How to do it...

How it works...

There's more...

Creating a Phong Specular type

Getting ready

How to do it...

How it works...

Creating a BlinnPhong Specular type

Getting ready

How to do it...

How it works...

See also

Creating an Anisotropic Specular type

Getting ready

How to do it...

How it works...

Physically-Based Rendering

Introduction

Understanding the metallic setup

Getting ready

How to do it...

How it works...

See also

Adding transparency to PBR

Getting ready

How to do it...

Semi-transparent materials

Fading objects

Solid geometries with holes

See also

Creating mirrors and reflective surfaces

Getting ready

How to do it...

How it works...

See also

Baking lights in your scene

Getting ready

How to do it...

Configuring the static geometry

Configuring the light probes

Baking the lights

How it works...

See also

Vertex Functions

Introduction

Accessing a vertex color in a Surface Shader

Getting ready

How to do it…

How it works…

There's more…

Animating vertices in a Surface Shader

Getting ready

How to do it…

How it works…

Extruding your models

Getting ready

How to do it…

How it works…

There's more…

Adding extrusion maps

Implementing a snow shader

Getting ready

How to do it…

How it works…

Coloring the surface

Altering the geometry

See also

Implementing a volumetric explosion

Getting ready

How to do it…

How it works…

There's more…

See also

Fragment Shaders and Grab Passes

Introduction

Understanding Vertex and Fragment Shaders

Getting ready

How to do it…

How it works…

There's more…

Input semantics

Output semantics

See also

Using the grab pass to draw behind objects

Getting ready

How to do it…

How it works…

There's more…

Implementing a Glass Shader

Getting ready

How to do it…

How it works…

There's more…

Implementing a Water Shader for 2D games

Getting ready

How to do it…

How it works…

Mobile Shader Adjustment

Introduction

Techniques to make shaders more efficient

Getting ready

How to do it...

How it works...

Profiling your shaders

Getting ready

How to do it...

How it works...

There's more...

Modifying our shaders for mobile

Getting ready

How to do it...

How it works...

Screen Effects with Unity Render Textures

Introduction

Setting up the screen effects script system

Getting ready

How to do it...

How it works...

There's more...

Using brightness, saturation, and contrast with screen effects

Getting ready

How to do it...

How it works...

Using basic Photoshop-like Blend modes with screen effects

Getting ready

How to do it...

How it works...

There's more...

Using the Overlay Blend mode with screen effects

How to do it...

How it works...

Behaviors – Intelligent Movement

Introduction

Creating the behaviors template

Getting ready

How to do it...

How it works...

There's more...

See also

Pursuing and evading

Getting ready

How to do it...

How it works...

Adjusting the agent for physics

Getting ready

How to do it...

How it works...

See also

Arriving and leaving

Getting ready

How to do it...

How it works...

Facing objects

Getting ready

How to do it...

How it works...

Wandering around

Getting ready

How to do it...

How it works...

Following a path

Getting ready

How to do it...

How it works...

There's more...

Avoiding agents

Getting ready

How to do it...

How it works...

There's more...

Avoiding walls

Getting ready

How to do it...

How it works...

There's more...

See also

Blending behaviors by weight

Getting ready

How to do it...

How it works...

There's more...

See also

Blending behaviors by priority

Getting ready

How to do it...

How it works...

There's more...

See also

Shooting a projectile

Getting ready

How to do it...

How it works...

There's more...

See also

Predicting a projectile's landing spot

Getting ready

How to do it...

How it works...

There's more...

Targeting a projectile

Getting ready

How to do it...

How it works...

There's more...

Creating a jump system

Getting ready

How to do it...

How it works...

Navigation

Introduction

Representing the world with grids

Getting ready

How to do it...

How it works...

There's more...

See also

Representing the world with points of visibility

Getting ready

How to do it...

How it works...

There's more...

Representing the world with a self-made navigation mesh

Getting ready

How to do it...

How it works...

Finding your way out of a maze with DFS

Getting ready

How to do it...

How it works...

There's more...

Finding the shortest path in a grid with BFS

Getting ready

How to do it...

How it works...

There's more...

Finding the shortest path with Dijkstra

Getting ready

How to do it...

How it works...

There's more...

Finding the best-promising path with A*

Getting ready

How to do it...

How it works...

There's more...

See also

Improving A* for memory – IDA*

Getting ready

How to do it...

How it works...

There's more...

Planning navigation in several frames – time-sliced search

Getting ready

How to do it...

How it works...

See also

Smoothing a path

Getting ready

How to do it...

How it works...

Decision Making

Introduction

Choosing through a decision tree

Getting ready

How to do it...

How it works...

There's more...

Implementing a finite-state machine

Getting ready

How to do it...

How it works...

There's more...

Improving FSMs: hierarchical finite-state machines

Getting ready

How to do it...

How it works...

See also

Implementing behavior trees

Getting ready

How to do it...

How it works...

See also

Working with fuzzy logic

Getting ready

How to do it...

How it works...

There's more...

See also

Making decisions with goal-oriented behaviors

Getting ready

How to do it...

How it works...

Implementing a blackboard architecture

Getting ready

How to do it...

How it works...

There's more...

Experimenting with Unity's animation state machine

Getting ready

How to do it...

How it works...

There's more...

The New NavMesh API

Introduction

Setting up the NavMesh building components

Getting ready

How to do it...

How it works...

There's more...

See also

Creating and managing NavMesh for multiple types of agents

Getting ready

How to do it...

How it works...

There's more...

Creating and updating NavMesh data at runtime

Getting ready

How to do it...

How it works...

Controlling the lifetime of the NavMesh instance

Getting started

How to do it...

How it works...

There's more...

Connecting multiple instances of NavMesh

Getting ready

How to do it...

How it works...

There's more...

Creating dynamic NavMeshes with obstacles

Getting ready

How to do it...

How it works...

There's more...

See also

Implementing some behaviors using the NavMesh API

Getting ready

How to do it...

How it works...

There's more...

Coordination and Tactics

Introduction

Handling formations

Getting ready

How to do it...

How it works...

There's more...

See also

Extending A* for coordination – A*mbush

Getting ready

How to do it...

How it works...

There's more...

Analyzing waypoints by height

Getting ready

How to do it...

How it works...

Analyzing waypoints by cover and visibility

Getting ready

How to do it...

How it works...

Creating waypoints automatically

Getting ready

How to do it...

How it works...

There's more...

See also

Exemplifying waypoints for decision making

Getting ready

How to do it...

How it works...

See also

Implementing influence maps

Getting ready

How to do it...

How it works...

There's more...

See also

Improving influence with map flooding

Getting ready

How to do it...

How it works...

See also

Improving influence with convolution filters

Getting ready

How to do it...

How it works...

There's more...

See also

Building a fighting circle

Getting ready

How to do it...

How it works...

There's more...

See also

Agent Awareness

Introduction

The seeing function using a collider-based system

Getting ready

How to do it...

How it works...

The hearing function using a collider-based system

Getting ready

How to do it...

How it works...

There's more...

The smelling function using a collider-based system

Getting ready

How to do it...

How it works...

There's more...

The seeing function using a graph-based system

Getting ready

How to do it...

How it works...

The hearing function using a graph-based system

Getting ready

How to do it...

How it works...

There's more...

See also

The smelling function using a graph-based system

Getting ready

How to do it...

How it works...

See also

Creating awareness in a stealth game

Getting ready

How to do it...

How it works...

There's more...

See also

Board Games and Applied Search AI

Introduction

Working with the game-tree class

Getting ready...

How to do it...

How it works...

See also

Implementing Minimax

Getting ready...

How to do it...

How it works...

See also

Implementing Negamax

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing AB Negamax

Getting ready...

How to do it...

How it works...

See also

Implementing NegaScout

Getting ready...

How to do it...

How it works...

See also

Implementing a Tic-Tac-Toe rival

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing a Checkers rival

Getting ready...

How to do it...

How it works...

There's more...

Implementing Rock-Paper-Scissors AI with UCB1

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing regret matching

Getting ready...

How to do it...

How it works...

There's more...

See also

Learning Techniques

Introduction

Predicting actions with an N-Gram predictor

Getting ready...

How to do it...

How it works...

There's more...

Improving the predictor – Hierarchical N-Gram

Getting ready...

How to do it...

How it works...

Learning to use Naïve Bayes classifier

Getting ready...

How to do it...

How it works...

Implementing reinforcement learning

Getting ready...

How to do it...

How it works...

Implementing artificial neural networks

Getting ready...

How to do it...

How it works...

Procedural Content Generation

Introduction

Creating mazes with Depth-First Search

Getting ready

How to do it...

How it works...

Implementing the constructive algorithm for dungeons and islands

Getting ready

How to do it...

How it works...

There's more...

See also

Generating landscapes

Getting ready

How to do it...

How it works...

Using N-Grams for content generation

Getting ready

How to do it...

How it works...

There's more...

See also

Generating enemies with the evolutionary algorithm

Getting ready

How to do it...

How it works...

There's more...

See also

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Through this Learning Path, you'll learn how to leverage the features of Unity and create amazing games, ranging from action shooters and mind-bending puzzle games to adventure and Virtual Reality (VR) games. If you have no prior experience of using Unity, you can still benefit from this Learning Path, which easily explains the complete working of the Unity toolset.

You'll start by learning how to create compelling shaders using Unity and understanding everything you need to know about vectors. This includes useful inputs on how lighting is constructed with vectors and how textures are used to create complex effects without the need for advanced math. In the succeeding chapters, you'll also be able to use popular AI techniques, such as A* and A*mbush to develop intelligent pathfinding agents for your games. The book will also guide you through different algorithms for creating decision-making agents that go beyond simple behaviors and movement.

By the end of this Learning Path, you will have developed all the basic skills to create highly engaging and replayable games.

This Learning Path includes content from the following Packt products:

Unity 2018 By Example - Second Edition by Alan Thorn

Unity 2018 Shaders and Effects Cookbook - Third Edition by John P. Doran, Alan Zucconi

Unity 2018 Artificial Intelligence Cookbook - Second Edition by Jorge Palacios

Who this book is for

If you are a game developer who wants to learn tools that can transform your gameplay, this beginner-level Learning Path is ideal for you. Having basic knowledge of C# will help you grasp the concepts explained in the book easily.

What this book covers

Chapter 1, Unity Fundamentals, begins our journey into Unity by creating a first-person collection game. This is a great starting point if you're totally new to Unity and are ready to create your first game.

Chapter 2, Creating a Collection Game, continues from the previous chapter and completes the first project. It assumes that you have completed the first chapter and brings a closure to our project, leading neatly to the next chapter.

Chapter 3, Post Processing Stack, introduces readers to the Post Processing Stack, which will allow users to tweak their game's appearance without having to write any additional scripts.

Chapter 4, Creating Your First Shader, introduces you to the world of shader coding in Unity. You will build some basic shaders and learn how to introduce tweakable properties in your shaders to make them more interactive.

Chapter 5, Surface Shaders and Texture Mapping, covers the most common and useful techniques that you can implement with Surface Shaders, including how to use textures and normal maps for your models.

Chapter 6, Understanding Lighting Models, gives you an in-depth explanation of how shaders model the behavior of light. This chapter teaches you how to create custom lighting models used to simulate special effects, such as toon shading.

Chapter 7, Physically-Based Rendering, shows you that physically-based rendering is the standard technology used by Unity 5 to bring realism to your games. This chapter explains how to make the most out of it by mastering transparencies, reflective surfaces, and global illumination.

Chapter 8, Vertex Functions, teaches you how shaders can be used to alter the geometry of an object. This chapter introduces vertex modifiers and uses them to bring volumetric explosions, snow shaders, and other effects to life.

Chapter 9, Fragment Shaders and Grab Passes, explains how to use grab passes to make materials that emulate the deformations generated by semi-transparent materials.

Chapter 10, Mobile Shader Adjustment, helps you optimize your shaders to get the most out of any device.

Chapter 11, Screen Effects with Unity Render Textures, shows you how to create special effects and visuals that would otherwise be impossible to achieve.

 

Chapter 12, Behaviors – Intelligent Movement, explores some of the most interesting movement algorithms based on the steering behavior principles developed by Craig Reynolds and work from Ian Millington. They act as a foundation for most of the AI used in advanced games and other algorithms that rely on movement, such as the family of pathfinding algorithms.

Chapter 13, Navigation, covers path-finding algorithms for navigating complex scenarios. It will include some ways of representing the world using different kinds of graph structure, and several algorithms for finding a path, each aimed to different situations.

Chapter 14, Decision Making, explains different decision-making techniques that are flexible enough to adapt to different types of games, and robust enough to let us build modular decision-making systems.

Chapter 15, The New NavMesh API, shows the inner workings of the NavMesh API introduced in Unity 5.6, and explains how it enables us to grasp the power of the NavMesh engine and tune it in real time.

Chapter 16, Coordination and Tactics, deals with a number of different recipes for coordinating different agents as a whole organism, such as formations and techniques that allow us make tactical decisions based on graphs, such as waypoints and influence maps.

Chapter 17, Agent Awareness, explores different ways to simulate sense stimuli on an agent. We will learn how to use tools we already know to create these simulations: colliders and graphs.

Chapter 18, Board Games and Applied Search AI, covers a family of algorithms for developing board games, as well as turn-based-game techniques for creating AI.

Chapter 19, Learning Techniques, explores the field of machine learning. It will give us a great head-start in our endeavor to learn and apply machine learning techniques into our games.

Chapter 20, Procedural Content Generation, explores different techniques for enabling replayability in our games by creating content procedurally. It will give us some pointers in the right direction for different types of content.

To get the most out of this book

Readers are expected to have experience of working with Unity and some scripting experience (C# or JavaScript is fine).

The book learning path is written with Unity 2018.1.0f2.

You need an internet connection for downloading the Python libraries. Also, basic knowledge of Python and networking is required.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Complete-Unity-2018-Game-Development. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Once selected, it will allow us to rename the item. Go ahead and set the name to BloomProfile."

A block of code is set as follows:

//-------------------------using UnityEngine;using System.Collections;//-------------------------public class Coin : MonoBehaviour

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "To create a build for desktop platform, select File | Build Settings from the File menu"

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Unity Fundamentals

This chapter starts the first project on our list, which will be a fun collection game. Remember, it doesn't matter if you've never used Unity before. We'll go through everything necessary step by step. By the end of the next chapter, you'll have pieced together a simple, but complete and functional, game. This is an important thing to achieve because you'll get familiar with a start-to-end game development workflow.

In this chapter, you will learn the following recipes:

Game design

Projects and folders

Asset importing and configuration

Level design

Game objects

Hierarchies

Game design

Let's make a coin collection game. Here, the player should control a character in the first-person mode, and he must wander the level, collecting all coins before a time limit runs out. If the timer runs out, the game is lost. On the other hand, if all coins are collected before the timer expires, the game is won. The first-person controls will use the default WASD keyboard setup, where W moves forward, A and S move left and right, and D walks backward. Head movement is controlled using the mouse, and coins are collected by simply walking into them. See Figure 1.1, featuring the coin collection game in action in the Unity Editor.

The great benefit in making this game is that it demonstrates all the core Unity features together and we don't need to rely on any external software to make assets, such as textures, meshes, and materials:

Figure 1.1: Preparing for a coin collection game (the completed game)

Getting started – Unity and projects

Every time you want to make a new Unity game, including coin collection games, you'll need to create New Project. Generally speaking, Unity uses the term Project to mean a Game. There are two main ways to make a new project, and it really doesn't matter which one you choose because both end up in the same place. If you're already in the Unity interface, looking at an existing scene or level, you can select File | New Project from the application menu. See Figure 1.2. It may ask if you want to save changes to the currently opened project and you should choose either Yes or No, depending on what you need.

After selecting the New Project option, Unity leads you to the project creation wizard:

Figure 1.2: Creating a new project via the main menu

Alternatively, if you've just started Unity for the first time, you'll probably begin at the welcome dialog. See Figure 1.3. From here, you can access the new project creation wizard by choosing the NEW PROJECT button:

Figure 1.3: The Unity welcome screen

On reaching the NEW PROJECT creation wizard, Unity can generate a new project for you on the basis of some basic settings. Simply fill in the name of your project (such as CollectionGame), and select a folder on your computer to contain the project files that will be generated automatically. Finally, select the 3D option from the template dropdown to create a 3D game, as opposed to 2D, and then click on the Create project button to complete the project generation process. See Figure 1.4:

Figure 1.4: Creating a new project

Projects and project folders

Unity has now created a blank, new, and empty project. This represents the starting point for any game development project and is the place where development begins. The newly created project contains nothing initially: no meshes, textures, or any other Assets. You can confirm this by simply checking the Project panel area at the bottom of the editor interface. This panel displays the complete contents of the project folder, which corresponds to an actual folder on your local drive created earlier by the project wizard. This folder should be empty. See Figure 1.5.

This panel will later be populated with more items, all of which we can use to build a game:

Figure 1.5: The Unity project panel docked at the bottom of the interface

If your interface looks radically different from Figure 1.5, in terms of its layout and arrangement, then you can reset the UI layout to its defaults. To do this, click on the Layout drop-down menu from the top-right corner of the editor interface, and choose Default. See Figure 1.6:

Figure 1.6: Switching to the default interface layout

Importing assets

Assets are the ingredients or building blocks for games—the building blocks from which they're made. Assets include meshes (or 3D models), such as characters, props, trees, houses, and more: textures, which are image files such as JPEGs and PNGs (these determine how the surface of a mesh should look); music and sound effects to enhance the realism and atmosphere of your game, and finally, scenes, which are 3D spaces or worlds where meshes, textures, sounds, and music live, exist, and work together holistically as part of a single system. Thus, games cannot exist without assets—they would otherwise look completely empty and lifeless. For this reason, we'll need assets to make the coin collection game we're working toward. After all, we'll need an environment to walk around in and coins to collect!

Unity, however, is a game engine and not primarily an asset creation program, like Blender or Photoshop (though it can create assets). This means that assets, such as characters and props, are typically made first by artists in external, third-party software. From here, they are exported and transferred ready-made to Unity, and Unity is responsible only for bringing these assets to life in a coherent game that can be played. Third-party asset creation programs include Blender (which is free of charge), Maya or 3DS Max to make 3D models, Photoshop or GIMP (which is free of charge) to create textures, and Audacity (which is free of cost) to generate audio. There are plenty of other options too. The details of these programs are beyond the scope of this book. In any case, Unity assumes that you already have assets ready to import to build a game. For the coin collection game, we'll use assets that ship with Unity. So, let's import these to our project.

To do this, select Assets | Import Package from the application menu. Then select Characters, ParticleSystems, Environment, and Prototyping. See Figure 1.9:

Figure 1.9: Importing assets via the Import Package menu

Starting a level

We've now created a Unity project and imported a large library of assets via the Unity Standard Asset package, including architectural meshes for walls, floors, ceilings, and stairs. This means that we're now ready to build our first level using these assets! Remember, in Unity, a scene means a level. The word scene and level can be used interchangeably here. They refer simply to a 3D space, that is, the space-time of the game world—the place where things exist. Since all games happen in space and time, we'll need a scene for the coin collection game. To create a new scene, select File | New Scene from the application menu or press Ctrl + N on the keyboard. When you do this, a new and empty scene is created. You can see a visualization or preview of the scene via the Scene tab, which occupies the largest part of the Unity interface. See Figure 1.14:

Figure 1.14: The Scene tab displays a preview of a 3D world
As shown in Figure 1.14, other tabs besides the scene are visible and available in Unity. These include a Game tab and an Animator tab; in some cases, there could be more as well. For now, we can ignore all the tabs except Scene. The Scene tab is designed for quick and easy previewing of a level during its construction.

Transformations and navigation

A scene with a floor mesh has been established, but this alone is uninteresting. We need to add more, such as buildings, stairs, columns, and perhaps more floor pieces. Otherwise, there would be no world for the player to explore. Before building on what we've got, however, let's make sure that the existing floor piece is centered at the world origin. Every point and location within a scene is uniquely identified by a coordinate, measured as an (X, Y, Z) offset from the world center (origin).

The current position for the selected object is always visible from the Object Inspector. In fact, the Position, Rotation, and Scale of an object are grouped together under a category (component) called Transform. Position indicates how far an object should be moved in three axes from the world center. Rotation indicates how much an object should be turned or rotated around its central axes. Scale indicates how much an object should be shrunk or expanded to smaller or larger sizes. A default Scale of one means that an object should appear at normal size, two means twice the size, and 0.5 means half the size, and so on. Together, the Position, Rotation, and Scale of an object constitute its transformation. To change the position of the selected object, you can simply type new values in the X, Y, and Z fields for Position. To move an object to the world center, simply enter (0, 0, and 0), as shown in Figure 1.20:

Figure 1.20: Centering an object to the world origin

Setting the position of an object, as we've done here, by typing numerical values is acceptable and appropriate for the specifying of exact positions. However, it's often more intuitive to move objects using mouse-based controls. To do this, let's add a second floor piece and position it away from the first instance. Drag and drop a floor piece from the Project panel in Scene to create a second floor GameObject. Then click on the new floor piece to select it and switch to the Translate tool. To do this, press W

Scene building

Now that we've seen how to transform objects and navigate the scene viewport successfully, let's proceed to complete our first level for the coin collection game. Let's separate the two floor meshes apart in space, leaving a gap between them that we'll fix by creating a bridge, which the player will be able to cross, moving between the floor spaces like islands. We can use the translate tool (W) to move objects around. See Figure 1.29:

Figure 1.29: Separating the floor meshes into islands

If you want to create more floor objects, you can use the method that we've seen already by dragging and dropping the mesh asset in the Project panel in the Scene viewport. Alternatively, you can duplicate the selected object in the viewport by pressing Ctrl + D on the keyboard. Both methods produce the same result.

Next, we'll add some props and obstacles to the scene. Drag and drop some house objects onto the floor. The house object (HousePrototype16x16x24) is found in the Assets | Standard Assets | Prototyping | Prefabs folder. See Figure 1.30:

Figure 1.30: Adding house props to the scene

On dragging and dropping the house in the scene, it may align to the floor nicely with the bottom against the floor, or it may not align like that. If it does, that's splendid and great luck! However, we shouldn't rely on luck every time because we're professional game developers! Thankfully, we can make any two mesh objects align easily in Unity using vertex snapping. This feature works by forcing two objects into positional alignment within the scene by overlapping their vertices at a specific and common point.

For example, consider Figure 1.31. Here, a house object hovers awkwardly above the floor and we naturally want it to align level with the floor and perhaps over to the floor corner. To achieve this, start by selecting the house object (click on it or select it from the Hierarchy panel). The object to be selected is the one that should move to align and not the destination (which is the floor), which should remain in place:

Figure 1.31: Misaligned objects can be snapped into place with Vertex Snapping

Next, activate the translate tool (W) and hold down the V key for vertex snapping. With V held down, move the cursor around and see how the Gizmo cursor sticks to the nearest vertex of the selected mesh. See Figure 1.32. Unity is asking you to pick a source vertex for the snapping:

Figure 1.32: Hold down V to activate Vertex Snapping

With V held down, move the cursor to the bottom corner of the house, and then click and drag from the corner to the floor mesh corner. The house will then snap align to the floor, corner to corner. When aligned this way, it releases the V key, and the two meshes get aligned exactly at the vertices. See Figure 1.33:

Figure 1.33: Align two meshes by vertices

Now you can assemble a complete scene using the mesh assets included in the Prototyping

Lighting and sky

The basic level has been created in terms of architectural models and layout; this was achieved using only a few mesh assets and some basic tools. Nevertheless, these tools are powerful and offer us a multitude of combinations and options to create great variety in game worlds. One important ingredient is missing for us, however. This ingredient is lighting. You'll notice from Figure 1.34 that everything looks relatively flat, with no highlights, shadows, or light or dark areas. This is because scene lighting is not properly configured for best results, even though we already have a light in the scene, which was created initially by default.

Let's start setting the scene for the coin collection game by enabling the sky, if it's not already enabled. To do this, click on the Extras drop-down menu from the top toolbar in the Scene viewport. From the context menu, select Skybox to enable Skybox viewing. A Skybox simply refers to a large cube that surrounds the whole scene. Each interior side has a continuous texture (image) applied to simulate the appearance of a surrounding sky. For this reason, clicking the Skybox option displays a default sky in the Scene viewport. See Figure 1.35:

Figure 1.35: Enabling the sky

Now, although the Skybox

Play testing and the Game tab

The environment created thus far for the coin collection game has been assembled using only the mesh assets included with the native Prototyping package. My environment, as shown in Figure 1.40, features two main floor islands with houses, and the islands themselves are connected together by a stepping-stone bridge. Your version may be slightly different, and that's fine: Figure 1.40:

Figure 1.40: The scene created so far contains two island areas

Overall, the scene is good work. It's well worth saving (remember to save regularly!). To save the scene, press Ctrl + S on the keyboard or else choose File | Save Scene from the application menu. See Figure 1.41.

If you're saving the scene for the first time, Unity displays a pop-up Save dialog, prompting you to name the scene descriptively (we called it Level_01):

Figure 1.41: Saving a scene

After saving the scene, it becomes scene asset of the project and appears in the Project panel. See Figure 1.42. This means that the scene is now a genuine and integral part of the project and not just a temporary work-in-progress as it was before. Notice also that saving a scene is conceptually different from saving a project.

For example, the application menu has entries for Save Scene and Save Project. Remember, a Project is a collection of files and folders, including assets and scenes. A scene, by contrast, is one asset within the project and represents a complete 3D map that may be populated by other assets, such as meshes, textures, and sounds. Thus, saving a project saves the configuration between files and assets, including scenes. Saving a scene, in contrast, just retains the level changes within that specified scene.

Figure 1.42: Saved scenes are added as assets within your project
You can see from Figure 1.42 that I've saved my scene in a folder named scenes. Folders can be created in your project by right-clicking on any empty area in the Project panel and choosing New Folder from the context menu, or else choose Assets | Create | Folder from the application menu. You can easily move and rearrange assets among folders by simply dragging and dropping them.

Now, the level, as it stands, contains nothing really playable. It's simply a static, lifeless, and non-interactive 3D environment made using the Editor

Adding a water plane

The collection game is making excellent progress. We now have something playable insofar as we can run around and explore the environment in first-person mode. However, the environment could benefit from additional polish. Right now, for example, the floor meshes appear suspended in mid-air with nothing beneath them to offer support. See Figure 1.50. Further, it's possible to walk over the edge and fall into an infinite drop. So, let's add some water beneath the floors to complement the scene as a complete environment:

Figure 1.50: The world floor appears to float and have no support

To add water, we can use another ready-made Unity asset included in the Project panel. Open the Standard Assets | Environment | Water | Water | Prefabs folder. Then drag and drop the WaterProDaytime asset from the Project panel in the scene. See Figure 1.51. This appears as a circular object, which is initially smaller than needed:

Figure 1.51: Adding water to the environment

After adding the Water prefab, position it below the floor level and use the scale tool (R) to increase its planar size (X, Z) to fill the environment outward into the distant horizon. This creates the feel that the floor meshes are smaller islands within an expansive world of water. See Figure 1.52:

Figure 1.52: Scaling and sizing water for the environment

Now, let's take another test run in the Game tab. Press play on the toolbar and navigate the character around in first-person mode. See Figure 1.53. You should see the water in the level. Of course, you can't walk on the water! Neither can you swim or dive beneath it. If you try walking on it, you'll simply fall through it, descending into infinity as though the water had never been there. Right now, the water is an entirely cosmetic feature, but it makes the scene look much better:

Figure 1.53: Testing the environment with water in FPS mode

The water is really a substanceless, ethereal object through which the player can pass easily. Unity doesn't recognize it as a solid or even a semi-solid object.

For now, however, we can add solidity to the water by first selecting the Water object from the Hierarchy panel (or in the Scene viewport) and then by choosing Component | Physics | Box Collider from the application menu. See Figure 1.54. Attaching a component to the selected object changes the object itself; it changes how it behaves. Essentially, components add behavior and functionality to objects, making them behave in different ways. Even so, resist the temptation to add lots of components to an object without reason and with the view that it makes them more versatile or powerful. It's better to have as few components on an object as necessary. This strategy of preferring relevant simplicity keeps your workflow neater, simpler, and optimized:

Figure 1.54: Attaching a Box Collider to a Water object

When a Box Collider is added to the water, a surrounding green cage or mesh appears. This approximates the volume and shape of the Water object and represents its physical volume, namely, the volume of the object that Unity recognizes as solid. See Figure 1.55:

Figure 1.55: Box Colliders approximate physical volume

If you play the game now, your character will walk on water as opposed to falling through. True, the character should be able to swim properly, but walking might be better than falling. To achieve full swimming behavior would require significantly more work and is not covered here. If you want to remove the Box Collider functionality and return the water back to its original, ethereal state, then select the Water object, click on the cog icon on the Box Collider component, and then choose Remove Component from the context menu. See Figure 1.56:

Figure 1.56: Removing a component

Adding a coin to collect

On reaching this far, our game has many features, namely, a complete environment, first-person controller, and water. However, we're supposed to be making a coin collection game and there aren't any coins for the player to collect yet. Now, to achieve fully collectible coins, we'll need to write some C# script, which will happen in the next chapter of this book. However, we can at least get started here at creating the coin object itself. To do this, we'll use a Cylinder primitive that's scaled to form a coin-looking shape. To create a cylinder, select GameObject | 3D Object | Cylinder from the application menu:

Figure 1.57: Create a Cylinder

Initially, the cylinder looks nothing like a coin. However, this is easily changed by scaling non-uniformly in the Z axis to make the cylinder thinner. Switch to the scale tool (R) and then scale the Cylinder inward. See Figure 1.58:

Figure 1.58: Scaling the cylinder to make a collectible coin

After rescaling the coin, its collider no longer represents its volume. It appears much larger than it should (see Figure 1.58). By default, the Cylinder is created with a Capsule Collider as opposed to a Box Collider. You can change the size of the Capsule Collider component by adjusting the Radius field from the Object Inspector when the coin is selected. Lower the Radius field to shrink the collider to a more representative size and volume. See Figure 1.59. Alternatively, you could remove Capsule Collider altogether and add Box Collider instead. Either way is fine; generally choose the simpler shape where possible. The colliders will be used in script in the next chapter to detect when the player collides with the coin to collect them:

Figure 1.59: Adjusting the Capsule Collider for the coin

Here we are! We now have the basic shape and structure for a coin. We will, of course, improve it carefully and critically in many ways in the next chapter. For example, we'll make it collectible and assign it a material to make it look shiny. However, here, using only a basic Unity primitive and scale tool, we're able to generate a shape that truly resembles a coin.