Book Image

Procedural Content Generation for Unity Game Development

By : Ryan Watkins
Book Image

Procedural Content Generation for Unity Game Development

By: Ryan Watkins

Overview of this book

Procedural Content Generation is a process by which game content is developed using computer algorithms, rather than through the manual efforts of game developers. This book teaches readers how to develop algorithms for procedural generation that they can use in their own games. These concepts are put into practice using C# and Unity is used as the game development engine. This book provides the fundamentals of learning and continued learning using PCG. You'll discover the theory of PCG and the mighty Pseudo Random Number Generator. Random numbers such as die rolls and card drafting provide the chance factor that makes games fun and supplies spontaneity. This book also takes you through the full development of a 2D game. Starting with level generation, you'll learn how PCG can make the game environment for you. You'll move into item generation and learn the different techniques to procedurally create game items. Thereafter, you'll be guided through the more abstract PCG areas such as scaling difficulty to the player and even generating music! The book helps you set up systems within your games where algorithms create computationally generated levels, art assets, quests, stories, characters, and weapons; these can substantially reduce the burden of manually creating every aspect of the game. Finally, you'll get to try out your new PCG skills on 3D terrain generation.
Table of Contents (18 chapters)
Procedural Content Generation for Unity Game Development
Credits
Disclaimer
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

This book is an introduction to Procedural Content Generation (PCG) and how it can be applied in the popular game engine, Unity3D. PCG is a powerful programming practice that is trending in modern video games. Though PCG is not a new practice, it has become even more powerful as technology has advanced and it looks to be a prominent component of future video games.

Throughout the course of this book, we will be learning the basis of procedural content generation, including theory and practice. You will start by learning what PCG is and what its uses are. You will then move into learning about pseudo random numbers and how they work with PCG to create unique gameplay.

After your introduction to PCG, you will dive in and build the core functionality of a 2D Roguelike game. This game will be heavily based on PCG practices so that you can experience what it takes to design and implement PCG algorithms. You will experience level generation, item generation, adaptive difficulty, music generation, and more. Lastly, we will move into 3D object generation by generating a 3D planet.

The aim of this book is to teach you about the theory of PCG while providing some simplified practical examples. By the end of the book, you should have a fundamental understanding of PCG and how it can be applied using Unity3D. This will all facilitate your further learning, research, and practice of PCG methods in video game development.

What this book covers

Chapter 1, Pseudo Random Numbers, teaches you about the theory of procedural content generation (PCG). We will cover what PCG is and how it is used in video games. You will then learn about a useful component of randomization called Pseudo Random Numbers (PRN). We will cover what PRNs are, how they are used, and how they can help us implement PCG algorithms.

Chapter 2, Roguelike Games, teaches you about a prime example of procedural content generation, Roguelike games. We will cover some history of the origin of PCG and Roguelike games. We will then set up the Unity project foundation of our very own Roguelike game.

Chapter 3, Generating an Endless World, begins the implementation of your 2D Roguelike game. We will be creating a level that generates itself at runtime while the player explores. We will cover PCG algorithm design and useful data substructures. Then, we will put it together to implement the game world.

Chapter 4, Generating Random Dungeons, implements the sublevels of our Roguelike game. We will cover a different approach to level generation as we generate a full level layout at runtime. You will learn about some common approaches to this technique and implement one for yourself.

Chapter 5, Randomized Items, teaches you about randomly generating items. The items you generate will have differing properties so we will use some techniques to communicate this to the player. We will cover item spawning, interaction, and inventory storage.

Chapter 6, Generating Modular Weapons, teaches you about and how to implement a random modular weapon system. You will build upon what you learned in the previous chapter to add more complexity to item generation. These items will comprise a small set of pieces that are assembled at runtime.

Chapter 7, Adaptive Difficulty, crosses over into the field of Artificial Intelligence (AI) and teaches you about how AI and PCG are similar and related. You will learn about the PCG idea of adaptive difficulty, which is one part AI and one part PCG. You will then implement an adaptive difficulty system for your Roguelike game.

Chapter 8, Generating Music, shows you how PCG can even contribute to the music and sound content of a game. You will learn a little music theory; just enough to design a PCG algorithm for music generation. Then, you will implement a music generator for your Roguelike game that can generate music at runtime.

Chapter 9, Generating a 3D Planet, switches gears from 2D-based PCG to 3D-based PCG. We will have finished our core 2D Roguelike functionality and be working on a new project. This chapter will introduce the fundamentals of 3D object generation. You will then implement a 3D planet generator. Plus, as a bonus, you will implement a first person controller to take a closer look at your generated world.

Chapter 10, Generating the Future, discusses the most common methods of PCG used today and some ways to further your learning in the subject. We will also summarize some of the key points of what you learned throughout the book and how they relate to these PCG methods. We will lastly take a look at some ways that we can improve these PCG methods for the future.

What you need for this book

This book uses the popular game engine Unity3D for all of its programming example implementations. At the time this book was written, Unity 5 was the current software version and all the code examples were written with Unity version 5.2.2. All of the code examples are written in the C# language.

You will need to download and install Unity3D onto your computer to follow the examples in this book. All that is required for this is Unity, as it will compile and run your code. It also comes with a code editor, MonoDevelop, which can be used to write your code. If you choose to use a different code editor, you may do so as well.

Who this book is for

This book was written with Unity development beginners in mind, but it is best suited for intermediate Unity developers. You will get the most out of this book if you are familiar with Unity development and the C# language. However, there is plenty of theory and programming method information that a beginning user can benefit from as well. Throughout the book, there are reference links and information tips that will guide an inexperienced user to additional information that will help facilitate their learning.

Conventions

In this book, you will find a number of styles of text 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: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

1 int integerVariable = 42;
2 
3 int interger function (int inInteger) {
4    return inInteger + 42;
5 } 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

1 int integerVariable = 42;
2 
3 int interger function (int inInteger) {
4     return inInteger + 42;
5 } 

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the Add component button and add Box 2D Collider and Sprite Renderer components".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on www.packtpub.com/authors.

Customer support

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.

Downloading the example code

You can download the example code files for all Packt books you have purchased 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.

Downloading the color images of this book

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/B04808_ColoredImages.pdf.

Errata

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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 with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it