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

Introducing PCG


We begin our learning adventure with the broad concept of PCG. The key word here is procedural. A procedure in programming, simply put, is an instruction to be executed. Procedures are the main paradigm in computer programming. A script you write in Unity is just a set of instructions or procedures we want Unity to perform.

You use procedures, methods, or functions as a means to communicate the instructions you want the computer to complete. We can use these same procedures to instruct the computer to generate content in many different ways. We can apply this idea to a broad range of programming disciplines such as data visualization, dynamic advertising, and so on, but in this book, we are using it for video games.

If procedural is the how then content is the what. Content can be anything we are presenting to the user. In our Hello World example later in the chapter, our content will simply be text. However, video games have a wide range of assets that make up the content we want to deliver to a player.

Typically, we think of the levels, character models, and other art assets when we think of content in video games. But there is also textures, music, sounds, story, artificial intelligence, and more that together make up the content of a game. PCG is the concept or paradigm by which all these pieces of content can be generated with some well-written code. PCG can be applied to nearly all aspects of a game through scripting, and you will learn some of the main ways to do this throughout the book.

On the left is a hand-drawn texture, and on the right is a Procedurally Generated texture

What's exciting about PCG is that we can let the computer take some of the responsibilities of the designer by giving it some instructions and letting it create parts of the game world on its own. We might even be surprised by the results. As developers, we usually do not like being surprised by our script's actions, but in this case, it's part of the plan.

PCG can also come in a few different forms for practical use. We can generate content from scratch, such as the texture see earlier, or we can generate assets from a set of premade parts, such as generating a tavern scene from premade props such as tables, chairs, barrels, and crates. Another option, though, is providing tools to the player to take on the role of creating content. The player creating content isn't necessarily PCG but you will have created a PCG system that now takes user input as a parameter. A great example of this is the popular game Minecraft developed by Mojang.

A player-created building in the popular game Minecraft

Minecraft is also an example of one of the most popular uses of PCG, randomization. Players in Minecraft can make structures and break down the land around them. However, the game's entire landscape is based on randomization. Randomization is used in many games, both virtual and table top. Randomness introduces a chance factor that creates fun out of unpredictability.

However, the most important thing about randomness in video games is that it is almost impossible to achieve true randomness on a computer system. This is why we refer to them as pseudo random numbers, because they are technically not random. We will explore this aspect of randomness, or pseudo randomness, later in the chapter with PRNs.