Unity 2018 by Example Learn About Game and Virtual Reality Development by Creating Five Engaging Projects Second Edition Alan Thorn
Unity 2018 by Example Learn About Game and Virtual Reality Development by Creating Five Engaging Projects Second Edition Alan Thorn
com
https://textbookfull.com/product/unity-2018-by-example-
learn-about-game-and-virtual-reality-development-by-
creating-five-engaging-projects-second-edition-alan-thorn/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/blazor-webassembly-by-example-second-
edition/
textboxfull.com
Hands On Game Development Patterns With Unity 2019 Create
Engaging Games by Using Industry Standard Design Patterns
With C 1st Edition David Baron
https://textbookfull.com/product/hands-on-game-development-patterns-
with-unity-2019-create-engaging-games-by-using-industry-standard-
design-patterns-with-c-1st-edition-david-baron/
textboxfull.com
https://textbookfull.com/product/deep-dive-into-power-automate-learn-
by-example-1st-edition-mishra/
textboxfull.com
https://textbookfull.com/product/moving-from-unity-to-godot-an-in-
depth-handbook-to-godot-for-unity-users-alan-thorn/
textboxfull.com
Unity 2018 By Example
Second Edition
Alan Thorn
BIRMINGHAM - MUMBAI
Unity 2018 By Example
Second Edition
Copyright © 2018 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.
Commissioning Editor: Kunal Chaudhari
Acquisition Editor: Larissa Pinto
Content Development Editor: Onkar Wani
Technical Editor: Ralph Rosario
Copy Editor: Safis Editing
Project Coordinator: Devanshi Doshi
Proofreader: Safis Editing
Indexer: Rekha Nair
Graphics: Jason Monteiro
Production Coordinator: Aparna Bhagat
ISBN 978-1-78839-870-1
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
• Learn better 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
PacktPub.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.PacktPub.
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.PacktPub.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
[i]
Table of Contents
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Preface
Video games are a cultural phenomenon that has captivated, entertained, and moved
billions of people worldwide over the past 50 years. As an industry and movement,
video games are an exciting place to be, both for the developer and the artist. In
these roles, your vision, ideas, and work can influence wide audiences, shaping
and changing generation after generation in an unprecedented way. In more recent
times, there's been a general movement toward democratizing game development,
making the development process simpler, smoother, and more accessible to a wider
audience, including developers perhaps working from home on a very limited
budget. Instrumental in this movement is the Unity engine, which forms the main
subject of this book. The Unity engine is a computer program that works with your
existing asset pipeline (such as 3D modeling software) and is intended for compiling
video games that work seamlessly across multiple platforms and devices, including
Windows, Mac, Linux, Android, iOS, and Windows Phone. Using Unity, developers
import ready-made assets (such as music, textures, and 3D models), and assemble
them into a coherent whole, forming a game world that works by a unified logic.
Unity is an amazing program. The latest version is free for most people to download
and use, and it works well with many other programs, including free software such
as GIMP and Blender. This book focuses on the Unity engine and how it can be used
in a practical context for making playable and fun games. No prior knowledge of
Unity is expected, although some knowledge of programming and scripting (such as
JavaScript, ActionScript, C, C++, Java, or C#) would be beneficial. Let's now take a
look at what this book covers, on a chapter-by-chapter basis.
Preface
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, Creating A Space Shooter, marks the beginning of our second project,
focusing on the creation of a space shooter game. Here, we'll create a project in which
the player must shoot the oncoming enemies.
Chapter 4, Continuing the Space Shooter, completes the space shooter project, taking the
project from its state in the previous chapter and adding final touches to it.
Chapter 6, Continuing the 2D Adventure, completes the 2D adventure game project that
was started in the previous chapter, adding the final touches and linking it together
with the overarching game logic. This is a great place to see how multiple parts and
facets of a game come together to form a whole.
Chapter 8, Continuing with Intelligent Enemies, brings closure to the AI project started
in the previous chapter. Here, we'll see how to use finite-state machines to achieve
powerful intelligence functionality that'll help us in a variety of scenarios.
[ viii ]
Preface
Chapter 10, Completing the VR Game, completes the VR project by adding gameplay
elements, core functionality, and by creating a build.
Appendix, Test Your Knowledge Answers, the answers to the Test Your Knowledge
section of each chapter.
[ ix ]
Preface
Once the file is downloaded, please make sure that you unzip or extract the folder
using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/
PacktPublishing/Unity-2018-By-Example-Second-Edition. 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. For example; "Mount the downloaded WebStorm-10*.dmg disk image
file as another disk in your system."
When we wish to draw your attention to a particular part of a code block, the
relevant lines or items are set in bold:
[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
[x]
Preface
Bold: Indicates a new term, an important word, or words that you see on the screen,
for example, in menus or dialog boxes, also appear in the text like this. For example:
"Select System info from the Administration panel."
Get in touch
Feedback from our readers is always welcome.
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, http://www.packtpub.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 http://authors.packtpub.com.
[ xi ]
Preface
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!
[ xii ]
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.
This chapter will demonstrate the following topics:
• 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.
[1]
Unity Fundamentals
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)
[2]
Chapter 1
After selecting the New Project option, Unity leads you to the project
creation wizard:
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:
[3]
Unity Fundamentals
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:
[4]
Chapter 1
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
[5]
Unity Fundamentals
[6]
Chapter 1
You can view the contents of your project folder directly via either Windows
Explorer or Mac Finder, by right-clicking the mouse in the Project panel from the
Unity Editor to reveal a context menu, and from there, choose the Show in Explorer
(Windows) or Reveal in Finder (Mac) option. See Figure 1.7:
Figure 1.7: Displaying the project folder via the Project panel
[7]
Unity Fundamentals
Clicking on Show in Explorer displays the folder contents in the default system file
browser. See Figure 1.8. This view is useful to inspect files, count them, or back them
up. However, don't change the folder contents manually this way via Explorer or
Finder. Specifically, don't move, rename, or delete files from here, because doing so
can corrupt your Unity project irretrievably. Instead, delete and move files where
needed within the Project panel in the Unity Editor. This way, Unity updates its
metadata as appropriate, ensuring that your project continues to work properly:
Figure 1.8: Viewing the Project panel from the OS file browser
Viewing the project folder in the OS file browser will display additional
files and folders not visible in the Project panel, such as Library and
ProjectSettings, and maybe a Temp folder. Together, these are known as
the project metadata. This is not directly a part of your project per se, but
contains additional settings and preferences that Unity needs to work
properly. These folders and their files should not be edited or changed.
[8]
Chapter 1
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.
[9]
Unity Fundamentals
To do this, select Assets | Import Package from the application menu. Then select
Characters, ParticleSystems, Environment, and Prototyping. See Figure 1.9:
Each time you import a package from the menu, you'll be presented with an Import
dialog. Simply leave all settings at their defaults, and click on Import. See Figure 1.10:
[ 10 ]
Exploring the Variety of Random
Documents with Different Content
Vanha rouva asettui tavalliselle paikalleen ikkunan luona olevan
ompelupöydän ääreen; mutta tällä kertaa hän ei ottanut esille
tavanmukaista reikäompelustaan tai virkkuutaan, vaan kääri auki
kirjeen. Hänellä oli samalla kummallisen juhlallinen ilme. Hannan
sydäntä kouristi; hän aavisti jotain erikoista tapahtuneen.
»Rakas lapsi, ota tuo palli ja istuudu tänne ja kuuntele mitä minulla
on sinulle sanottavaa. Kuten tiedät, olen minä sinun suhteesi äidin
asemassa. Minun täytyy valvoa etuasi ja suojella sinua. Pelkään
sinun olevan… liian kiemailevan… Sinun ikäisenäsi olin tosin
minäkin kiemaileva, mutta ainoastaan sellaisille ihmisille, joille äitini
luvalla sain kiemailla. Olen vakuutettu, että sinulla ensi talvena on
tilaisuus keskiviikkokutsuissani tehdä sopivia valloituksia. Siellä on
esimerkiksi majuri, paroni Schimmerer, joka on leskimies ja omistaa
kolmikerroksisen talon jossain Wienin esikaupungissa…»
»Ja miksi en! Äiti sanoi aina, että minusta on tuleva kreivitär tai
miljoonain omistaja…»
Hanna oli tuskin jäänyt yksin, kun jo suli kyyneliin. Nyt hän vasta
selvästi tunsi rakastavansa nuorta tiedemiestä. Ja tämä… tämä oli
pyytänyt häntä vaimokseen; varmaankin riippui hänen elämänsä
vastauksesta… ja kummitäti aikoi musertaa hänen sydämensä
rukkasilla. Ei, tapahtukoon mitä hyvänsä, hän vastaa myöntyvästi.
»Sisään!»
»Tulevaisuuteni.»
»Vastaus on myöntävä.»
Hanna huokasi.
»Ei, ei… pukujen takia en epäile… mutta jos Ewald ei ole mukana,
on kai sopimatonta…»
»Oh, miten ikävä ilta!» hän huokaa. »Miten olet nyt huvitellut
lapsi?»
»Kuninkaallisesti!»
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com