Create an RPG Monster Spawner

In this tutorial, you will add a system to spawn monsters continually. You will also add a Boss monster to your game.

Adding a Monster Spawner

Create a way for enemies to spawn continually on the map.

Create a new script called MonsterSpawner.

Add a new object to the map and give it a box collider that spawns over the space you want enemies to spawn, attach the MonsterSpawner Script to the object.

Add the below code to the MonsterSpawner Script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterSpawner : MonoBehaviour

    public GameObject monsterPrefab;
    public float timeToSpawn = 10f;

    private void Start()
        InvokeRepeating("spawnMonster", timeToSpawn, timeToSpawn);

    private void spawnMonster()
        Instantiate(monsterPrefab, getRandomSpawnPoint(), Quaternion.identity, this.transform);

    private Vector3 getRandomSpawnPoint()
        Bounds bounds = GetComponent<BoxCollider>().bounds;
        float randomX = Random.Range(bounds.min.x, bounds.max.x);
        float randomZ = Random.Range(bounds.min.z, bounds.max.z);

        float height = Terrain.activeTerrain.SampleHeight(new Vector3(randomX, 0, randomZ));
        return new Vector3(randomX, height, randomZ);

Set up the remaining parameters, along with the enemy prefab. And test it. Enemies should spawn every ten seconds inside the box.

Final Boss Monster

Add a final boss that is hard to defeat

No RPG would be complete without an epic final boss fight. While not the flashiest way to do it, you can create a final boss just by taking the smaller enemy and making it larger and stronger.

Unpack the monster prefab, rename it BossMonster, and scale it up. Then, create new variables for the boss monster for its attack, max health, and exp. Consider giving the monster more HP than a regular monster so it is tougher to defeat.

Now, the player can fight smaller enemies until they are a high enough level to defeat the boss.

What's Next

This is only the beginning of an RPG game. There's a lot more to add to it, but that will come in future lessons. As more systems are added to the RPG, the ScriptableObject structure will become more and more useful for keeping the different parts of the code segmented from each other.