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

Get Procedural Content Generation for Unity Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.