O'Reilly logo

Procedural Content Generation for Unity Game Development by Ryan Watkins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required