Search Unity

Blackthornprod's "Random Dungeon Generation Tutorial"

Discussion in 'General Discussion' started by storiesbystrauss, Dec 9, 2019.

  1. storiesbystrauss

    storiesbystrauss

    Joined:
    Sep 10, 2018
    Posts:
    1
    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;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomSpawner : MonoBehaviour {
    6.    
    7.     public int openingDirection;
    8.     // 1 --> need bottom door
    9.     // 2 --> need top door
    10.     // 3 --> need left door
    11.     // 4 --> need right door
    12.  
    13.     private RoomTemplates templates;
    14.     private int rand;
    15.     private bool spawned = false;
    16.  
    17.     public float waitTime = 4f;
    18.  
    19.     void Start(){
    20.         Destroy(gameObject, waitTime);
    21.         templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    22.         Invoke("Spawn", 0.5f);
    23.     }
    24.  
    25.  
    26.     void Spawn() {
    27.         if(spawned == false){
    28.             if (openingDirection == 1){
    29.                 //Need to spawn a room with a BOTTOM DOOR.
    30.                 rand = Random.Range(0, templates.bottomRooms.Length);
    31.                 Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
    32.             } else if (openingDirection == 2){
    33.                 // Need to spawn a room with a TOP DOOR.
    34.                 rand = Random.Range(0, templates.topRooms.Length);
    35.                 Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
    36.             } else if (openingDirection == 3){
    37.                 // Need to spawn a room with a LEFT DOOR.
    38.                 rand = Random.Range(0, templates.leftRooms.Length);
    39.                 Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
    40.             } else if (openingDirection == 4){
    41.                 // Need to spawn a room with a RIGHT DOOR.
    42.                 rand = Random.Range(0, templates.rightRooms.Length);
    43.                 Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
    44.             }
    45.             spawned = true;
    46.         }
    47.     }
    48.  
    49.     void OnTriggerEnter2D(Collider2D other){
    50.         if (other.CompareTag("SpawnPoint")){
    51.             var roomSpawner = other.GetComponent<RoomSpawner>();
    52.  
    53.             if (roomSpawner == null){
    54.                 Debug.LogError(other.transform.name + "  has the tag spawnpoint but doesn't have a roomspawner script attached to it");
    55.                 return;
    56.             }
    57.             if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false){
    58.                 Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
    59.                 Destroy(gameObject);
    60.             }
    61.             spawned = true;
    62.         }
    63.     }
    64. }
     
    tigerleapgorge likes this.
  2. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    Wow, that's an awful tutorial.

    Try this one. Its far from perfect but you dont need to use collision to place new rooms.
    One thing it doesnt cover is exit room placement so you'll need to come up with how to achieve this yourself.
    It does allow you to specify number of rooms and seems to come up with interesting levels.
    One thing I don't like is how it decides where to place doors (ie if a room is next to another room then it gets a door) but its certainly a decent starting point.