I have recently completed Blackthornprod’s “Random Dungeon Generation Tutorial” and looking to expand upon it (set number of rooms, corridors, etc). My coding skills are limited but I needed a new way of making levels instead of hand painting a map with a Tile Palette (I tried to set up a Rule Tile but I’ve only ever gotten it to work for 2D platformers).
Currently my Dungeon spawns an endless amount of rooms to which I haven’t found what I’ve done differently from the tutorial. I would like to set a Min / Max of rooms to be spawned yet I am unsure of how to go about this as the other Random Generation tutorials use different methods.
Any advice, links to other tutorials, or even some coding edits would be greatly appreciated.
Thank you kindly!
Here’s a quick video to show you my issue
RoomSpawner script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomSpawner : MonoBehaviour {
public int openingDirection;
// 1 --> need bottom door
// 2 --> need top door
// 3 --> need left door
// 4 --> need right door
private RoomTemplates templates;
private int rand;
private bool spawned = false;
public float waitTime = 4f;
void Start(){
Destroy(gameObject, waitTime);
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Invoke("Spawn", 0.5f);
}
void Spawn() {
if(spawned == false){
if (openingDirection == 1){
//Need to spawn a room with a BOTTOM DOOR.
rand = Random.Range(0, templates.bottomRooms.Length);
Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
} else if (openingDirection == 2){
// Need to spawn a room with a TOP DOOR.
rand = Random.Range(0, templates.topRooms.Length);
Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
} else if (openingDirection == 3){
// Need to spawn a room with a LEFT DOOR.
rand = Random.Range(0, templates.leftRooms.Length);
Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
} else if (openingDirection == 4){
// Need to spawn a room with a RIGHT DOOR.
rand = Random.Range(0, templates.rightRooms.Length);
Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
}
spawned = true;
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag("SpawnPoint")){
var roomSpawner = other.GetComponent<RoomSpawner>();
if (roomSpawner == null){
Debug.LogError(other.transform.name + " has the tag spawnpoint but doesn't have a roomspawner script attached to it");
return;
}
if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false){
Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
Destroy(gameObject);
}
spawned = true;
}
}
}