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

Adding enemies to the world board


As usual, we will need to generate the enemies at random to keep the player on their toes. We then need to address the issue of enemies being able to move through black space. We will also have to handle the event that an enemy moves off screen.

Let's start by adding the enemies to the world board. Open up the BoardManager.cs script for editing. You can start by adding the line public GameObject enemy; under all the other public variables. This will be our Enemy prefab reference. Then, take a look at Code Snip 7.2 for the rest of the update:

1 private void addTiles(Vector2 tileToAdd) {
2   if (!gridPositions.ContainsKey (tileToAdd)) {
3    gridPositions.Add (tileToAdd, tileToAdd);
4    GameObject toInstantiate = floorTiles [Random.Range (0, floorTiles.Length)];
5    GameObject instance = Instantiate (toInstantiate, new Vector3 (tileToAdd.x, tileToAdd.y, 0f), Quaternion.identity) as GameObject;
6    instance.transform.SetParent (boardHolder);
7
8    if (Random...