38,39 €
Create your own augmented reality games from scratch with Unity 5
This book is for those who have a basic knowledge of game development techniques, but no previous knowledge of Unity is required. Some basic programming knowledge would be desirable, but the book is an introduction to the topic. The book is also suitable for experienced developers new to GIS or GPS development.
The heyday of location-based augmented reality games is upon us. They have been around for a few years, but the release of Pokemon Go was a gamechanger that catalyzed the market and led to a massive surge in demand. Now is the time for novice and experienced developers alike to turn their good ideas into augmented reality (AR) mobile games and meet this demand!
If you are keen to develop virtual reality games with the latest Unity 5 toolkit, then this is the book for you. The genre of location-based AR games introduces a new platform and technical challenges, but this book will help simplify those challenges and show how to maximize your game audience.
This book will take you on a journey through building a location-based AR game that addresses the core technical concepts: GIS fundamentals, mobile device GPS, mapping, map textures in Unity, mobile device camera, camera textures in Unity, accessing location-based services, and other useful Unity tips. The technical material also discusses what is necessary for further development to create a multiplayer version of the game. At the end, you will be presented with troubleshooting techniques in case you get into trouble and need a little help.
This book shows you how to create every step of the game and gives practical examples.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 396
Copyright © 2017 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 author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be 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 2017
Production reference: 1160117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78712-288-8
www.packtpub.com
Author
Micheal Lanham
Copy Editor
Safis Editing
Reviewer
Derek Lam
Project Coordinator
Ritika Manoj
Commissioning Editor
Amarabha Banerjee
Proofreader
Safis Editing
Acquisition Editor
Reshma Raman
Indexer
Tejal Daruwale Soni
Content Development Editor
Arun Nadar
Graphics
Jason Monteiro
Technical Editor
Rupali R. Shrawane
Production Coordinator
Shraddha Falebhai
Micheal Lanham is a solutions architect with petroWEB and currently resides in Calgary, Alberta in Canada. In his current role he develops integrated GIS applications with advanced spatial search capabilities. He has worked as a professional and amateur game developer building desktop and mobile games for over 15 years. In 2007, Micheal was introduced to Unity 3D and has been an avid fan and developer ever since.
To the people I think about every day. My everything, Rhonda and children: Colton, Breann, Mikayla, and Charliegh.
Derek Lam is a game designer and Unity Certified Developer who has experience in game design on iOS and Android for over 5 years. In addition, he has a lot of experience in developing augmented reality as well as virtual reality applications. He's currently working in a construction company, producing AR and VR interactive applications, mostly for internal usage.
For support files and downloads related to your book, please visit www.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.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].
At the beginning of 2016, most of the world had very little knowledge of augmented reality and location-based games. That, of course, all changed with the release of Pokemon Go later that year. Literally overnight, the genre became entrenched as an upcoming trend in game development. Chances are you have played Pokemon Go and the reason you are reading this book is because of your interest in the genre of AR and location-based games.
In this book we will explore in detail the aspects of creating a location-based AR game just like Pokemon Go. Location-based AR games are expensive and require multiple services for everything from mapping to spawning monsters. However, the game we develop will be done with zero budget using freely available services. While this may not be something you could release commercially, due to some licensing restrictions, it will certainly introduce you to most of the concepts. Along the way, you will also learn how to use a great tool, Unity, and introduce many other concepts in game development.
Chapter 1, Getting Started, introduces the concepts that make up the genre of location-based AR games and our fictional game, Foody Go. This will be followed by a walk-through of downloading all the required software and setting up your mobile development environment with Unity.
Chapter 2, Mapping the Player's Location, starts by introducing the fundamental concepts of GIS, GPS, and mapping. Then shows how those concepts are applied to generating a real-time map and plotting the player’s location in a game.
Chapter 3, Making the Avatar, builds on the previous chapter and transforms our simple location marker into a moving animated character. This allows the player to see their avatar move around the map as they move carrying their mobile device.
Chapter 4, Spawning the Catch, explains that the premise of Foody Go is about catching experimental monsters. In this chapters, we learn how to spawn the monsters around the player on the map.
Chapter 5, Catching the Prey in AR, ups the intensity by introducing integrated AR part of the game by accessing the device's camera, introduces physics for throwing balls, tracking player swiping, using creature reactions, and working with a new game scene.
Chapter 6, Storing the Catch, devoted to developing the player's inventory bag, which will hold all the Foody creatures they catch, and other useful items. Here, we walk the reader through adding persistent storage and adding a simple inventory scene.
Chapter 7, Creating the AR World, adds locations of interest around the player based on a real-time data service.
Chapter 8, Interacting with an AR World, allows the player to interact with the locations of interest. In our simple game, the player will be able to sell their caught monsters.
Chapter 9, Finishing the Game, provides the reader with the information on how to finish the game or better yet write their own location-based AR game. For the purposes of this book, we will only develop the demo Foody Go game.
Chapter 10, Troubleshooting, cover a number of troubleshooting tips and tricks to overcome those development obstacles. As with any software development exercise, problems always arise.
In order to follow all the exercises in this book you will need, at a minimum, a computer capable of running Unity 5.4+ and an iOS or Android device capable of running Unity games and equipped with a GPS.
More details about Unity system requirements may be found at: https://unity3d.com/unity/system-requirements.
This book is intended for anyone with an interest in developing their own Pokemon Go, location-based AR game. While this book assumes no prior game development skills or Unity experience, you will need a basic understanding of the C# language or equivalent (C, C++, Java, or JavaScript).
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The next lines of code read the link and assign it to the to the BeautifulSoup function."
A block of code is set as follows:
#import packages into the project from bs4 import BeautifulSoup from urllib.request import urlopen import pandas as pdWhen we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
<head> <script src="d3.js" charset="utf-8"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head>Any command-line input or output is written as follows:
C:\Python34\Scripts> pip install -upgrade pipC:\Python34\Scripts> pip install pandasNew terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go to Files | Settings | Project Name | Project Interpreter."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
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/Augmented-Reality-Game-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/AugmentedRealityGameDevelopment_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.[footnote]
This chapter will introduce you to real-world adventure games—what they are, how they work, and what makes them unique. From there, we will introduce the sample real-world game that we will build through the rest of the book. Finally, after the theory, we will do a brisk walk-through on setting up a mobile development environment with Unity.
For those of you who feel that they understand the terminology of real-world adventure or augmented reality games, feel free to jump ahead to the Introducing Foody GO section of this chapter that will discuss the game design and concept of the sample game we will be building throughout the book.
In this chapter, we will cover the following topics:
Real-world adventure games are a genre of games that has surged in popularity recently with the release of Pokemon GO. Chances are, at the time of reading this book, you may have certainly heard of, and likely have played, the popular game. Although many think this genre is an overnight sensation, it has in fact been around for several years. Niantic, the developer of Pokemon GO, released Ingress, its first real-world game, in November 2012. The title was, and is, popular, but has only attracted a niche following of gamers, which was likely more the result of the game's complex theme rather than being specific to the genre.
Now, many can suggest that the primary catalyst that launched Pokemon GO into a gaming sensation is the Pokemon franchise combined with a new augmented reality gaming platform. Certainly, without its integral real-world interaction, Pokemon GO would have been just another popular mobile game.
So, what are the elements that make a real-world adventure or location-based augmented reality game unique?
Of course, there are many other elements that will be needed to create a successful game, but essentially, location-based and augmented reality are the elements that identify the real-world adventure genre. Astute readers may notice that massively multiplayer network gameplay or MMO was omitted. Although MMO gameplay may be essential to certain game designs, it is not a requirement of this genre.
Tracking a player's location in the real world and then overlaying that into the game's virtual world creates a unique level of immersion for players. In fact, in many real-world adventure games, warning messages are presented to players before they start playing. There are many stories of players becoming so immersed that they have hurt themselves due to an avoidable accident while playing a real-world game.
Mapping the real world on top of the game's virtual world provides new challenges to traditional mobile gaming. Developing a map interface and populating it with virtual items require some advanced GIS skills. Many developers will be new or relatively inexperienced in the concepts of GPS and GIS or rendering a map in Unity. Since mapping is a core concept to the real-world genre, it will be the basis for much of the sample game we will be building. We will spend several chapters related to the topic of maps. The following is a list of chapters that will touch on mapping and location:
AR has been around since 1990. The term typically covers a broad range of technologies from virtual surgical devices, Microsoft HoloLens, and mobile apps such as Snapchat. AR technologies have been slow to become mainstream in gaming until just very recently. With the advance of new technologies and the real-world adventure genre being major contributors to increasing popularity of AR in gaming.
As mentioned, AR covers a broad range of technologies or devices that provide an overlaid virtual environment to the user. However, on a mobile device, the AR experience is often a result of rendering a virtual environment over a backdrop of the device's camera. In some cases, the AR game or application will have sophisticated image processing algorithms that identity features. Those identified features may then be annotated virtually with other graphics or game options. Pokemon GO, for instance, limits the use of AR to just the camera background, whereas Snapchat provides a dynamic AR experience to the user through the use of image processing. Yet, both the game and application benefit by providing the user with a more enjoyable experience through AR.
For the purpose of the real-world adventure genre and this book, we will take a basic approach to provide the user with a basic AR experience. That means, we will look at integrating the mobile device's camera as a game backdrop. The gaming experience will be familiar to other popular games in the genre. Even with this basic approach to AR, we will still cover a number of other details and tips in the course of a few chapters. The following is a short description of the AR elements that we will work on in the subsequent chapters:
Adventure games are typically characterized by a quest-driven story where players must explore and solve puzzles in order to complete the game, whereas the current real-world adventure games are more about exploration than puzzle solving and completing a quest. If anything, the current batch of real-world games are more like a Role-Playing Game (RPG) than a classic adventure game. In the future, we may certainly see more true classic adventure games or possibly other real-world mixes, such as real-time strategy, shooters, simulation, educational, sports, and puzzle.
In order to demonstrate how all these new concepts come together, we will be building a sample game throughout the book. This game will loosely follow an adventure style not unlike other popular titles in the real-world genre. In this game, we will be adding many common game elements, such as an avatar, character inventory, particle effects, and more in the course of several chapters; the following is a short introduction to those chapters:
Certainly, the best way to learn any new or advanced concepts is by example. Foody GO will be our example real-world adventure game that we will be building throughout the book. The game will follow a food theme where the player will search for and catch experimental cooking monsters. Once caught, the player must take their monsters to local restaurants in order to sell them for items, power and prestige.
Of course, our sample game will focus on the location-based augmented reality elements, but we will introduce several other technical features, as follows:
We won't get into extensive detail on any of the preceding features, as most of these items could cover a book by itself. However, it will be helpful to understand how each of these elements comes together to make a real-world adventure game.
All of the books source code can be downloaded from SITE. The source code will be broken down chapter by chapter and provided as a progressive project. For each chapter, the starting and ending project's states will be provided. This will allow the more advanced reader to jump ahead in the book. Novice readers are encouraged that you follow all the examples in the book, as later chapters will be more advanced in content.
Now, with all the background set and our course laid out, let's get started by introducing mobile development with Unity. More advanced readers who have developed Unity games on Android or iOS may want to skip ahead to Chapter 2, Mapping the Player's Location.
This installation guide is intended to be cross-platform compatible and should work on Windows, Linux, or Mac. For the sake of brevity, only screenshots for the Windows platform will be shown.
Even if you have installed Unity before, but have not done mobile development, make sure that you follow this section closely. There are a couple of important steps you would not want to miss.
Perform the following steps to install Unity:
In the following example screenshot, we have selected both Android and iOS. Ensure that you only select the platform you need:
Choose only the components you need
Select the default installation path for Unity and then click on Next to install.Even with the limited number of components, the installation will still take several minutes, so grab a coffee and wait until it completes.
If you are using an Android device to test the game, use this section to get ready. Developers who already have experience with Android can briefly review this section or jump ahead to the Getting Started with Unity section in this chapter.
Always install the same architecture version, 64 or 32 bit.
Follow the subsequent steps to install the Android SDK on your development computer. Even if you already have the SDK installed, please review these steps to ensure that you have the right path and components set:
Always note where you install a development kit like the JDK or SDK.
Install the Android SDK component
For the installation location, change the path to something that is easy for you to remember and locate. In the example screenshot, Android/AndroidStudio and Android/AndroidSDK are used:Select an installation location that will be easy to find later on
After the installation completes, open Android Studio. Open the Android SDK Manager by selecting menu item Tools | Android | AndroidSDK. In the following example screenshot, only Android 5, API Level 21 is selected because it matches the device:Set the Android SDK location and select API level that matches your device
On the Android SDK panel, set the location path to the same as you used in step 5. Then, select to install Android API level that matches your Android device and click on Apply. Locate the Android version of your phone from Settings | About phone | Android version. This API installation may take several minutes, so this will be a good time for another coffee or the beverage of choice.After the API installation completes, close Android Studio.For best results when following the examples in this book, you will need to connect a physical device to your computer for testing. It is possible to emulate a GPS and camera in an Android emulator, but that is beyond the scope of this book. Follow these steps to get your device connected:
That completes the bulk of setting up an Android device. We still have a couple more settings to make in Unity, but we will cover that in the next section on project setup.
In order to keep the content of this book focused and the development platform independent, we will not provide a step-by-step guide here. However, there is an excellent guide for iOS setup on the Unity site at https://unity3d.com/learn/tutorials/topics/mobile-touch/building-your-unity-game-ios-device-testing.
After you complete your iOS setup, return to the book, and we will begin building the example game project.
Unity is a great platform to start learning game development or even shipping a commercial game. It is the choice of game engine for many of the popular games in the Android or iOS app stores. So, what makes Unity such a great platform to develop games on? The following is a short list of things that make Unity such a compelling platform to develop games on:
Let's get started by creating our sample game project Foody GO; we will also take this opportunity to build and deploy this starting project to your mobile device:
Editor layout for mobile development
Before we go further, let's take a look at each of the main windows we will be working with in Unity:Editor window with UI focused on in Scene window
The panel in the scene window is a 2D UI element that allows us to render text or other content to the player. By default, the panel centers itself to the main view of the camera when it is added to the scene. This is why we see the semitransparent panel cover the entire Game window. Since we don't want a transparent background to the splash screen, let's change the color.Select the Panel in the Hierarchy window. Then, click on the white box beside the Color property in the Inspector window to open the Color settings. You will see the following dialog:Enter FFFFFFFF in the Hex Color field and then close the dialog. Notice how the background of the Game window is now opaque white.For this chapter, we started out by introducing what the genre of real-world adventure games is and why it has become so popular. We then went into more detail on the major components that comprise the genre and how we will be covering each component in the book. After that, we introduced the sample game Foody GO that we will be building as a sample real-world adventure game. Then, we dove headfirst into installing Unity and the required dependencies for building, deploying, and testing it on your mobile device. Finally, we created the Foody GO game project and added a simple splash screen.
In the next chapter, we will continue building on the Foody GO game project and start to add mapping. However, before we add maps to our game, we will cover some basics on GPS and GIS.
The core of most real-world games is the location-based map. That integration of the player's real-world position mapped into the virtual world extends the game's virtual elements into the real world, allowing players to explore the world around them with a new perspective and sense of exploration.
In this chapter, we will take our first steps to understanding how to integrate maps into a Unity game. However, before working with Unity, we will cover the basics of GIS and GPS. This will allow us to establish some simple definitions and background for what can be complex topics. After that, we will dive back into Unity and add a location-based map, a basic character, and free look camera to our Foody GO project. Even though we are dealing with advanced concepts, we will keep things simple for now and avoid getting into any code. Of course, more advanced readers with a background in GIS have the options to explore the code at their leisure.
Here is a quick breakdown of what we will cover in this chapter:
GIS stands for geographic information system, a system in which geographic data is collected, stored, analyzed, manipulated, and served as maps. Although that definition is still prominent, GIS has come to mean everything from software applications, hardware, tools, science, and services. Google Maps, for example, is the best-known example of a GIS in use today. Yet, in this book, we will also use GIS to mean the science and process of converting geographic data and mapping.
Cartographers have been mapping the world around them for thousands of years. Yet only very recently with the development of computers did it become efficient to be used in computers and GIS to create dynamic maps. Unlike traditional hand-drawn paper maps, dynamically rendered maps are composed of layers of spatial data that could describe roads, points of interest, parks, boundaries, landscape, water, and more. As much as the common Google Maps user has little control over what layers are shown, they are there. The following is a figure that shows the typical layers that could comprise a road map:
Source: http://bit.ly/2iri2vr
Google Maps, Bing, and other GIS providers will typically render maps at various zoom levels and then cut those maps into static image tiles. The GIS server then serves those tiles to the user. This works well for performance, but provides few options for customization or styling, aside from adding shapes, lines, or points of interest. This form of tile mapping is often referred to as static mapping. For this book, we will work with the Google Maps API, which provides us with dynamic maps.
Dynamic maps give the presenter or developer options to style and/or symbolize the data as required. For instance, on some maps, you may want to alter parks to be shown as blue instead of green. This is the type of flexibility that dynamic maps provides. When we will add the Google Maps API to our project later in this chapter, we will explore custom style and symbolization options.
Now that we understand the basics of GIS mapping, let's get into understanding some terminologies and concepts. The following is a list of terms we use when describing or making maps:
The following is a diagram showing WGS 84 major lines of interest:
Source: http://bit.ly/2iOoPNG
Longitude west of the prime meridian is measured as negative. Latitude south of the equator is measured as negative.
Web Mercator projection on the left and Gall-Peters on the right
GPS is an acronym for global positioning system, a network of 24 to 32 satellites that orbit the earth twice every hour. As these satellites orbit, they emit time-encoded geographical signals as a beacon to any GPS device on earth that can see them. A GPS device then uses these signals to triangulate its position anywhere on the planet. The more satellites and signals a device can receive a signal from, the more accurate the location will be. We will get into more details about GPS triangulation and accuracy in Chapter 4, Spawning the Catch.
The following diagram shows a GPS device acquiring a signal from visible satellites on the network:
GPS Device tracking visible satellites
This is a list of terms we may come across when talking about or using GPS on a device:
As previously mentioned, we will use Google Maps for our map service. For this version of the game, we will use the Google Maps' static map, which means we do not need an API developer key or need to worry about its usage.