Hey, I’ve struggling for several hours with this issue and now decided to ask for help. I will provide my scripts and screenshots to give the best possible overview.
I am working on a dungeon generator which works pretty good, but there are two main issues. I have these dead ends (Screenshot by Lightshot) which allow the player to enter the Gamescene. To tackle that, I generate a box that blocks these openings (Screenshot by Lightshot), but this box creates the following problems:
- I can’t set a maximum number of spawned rooms. The box is being instanced from the same script as the other rooms, which means, that the maximum number also applies to the box. This is extremely annoying to me and I don’t know how to solve that.
- It’s not clean at all. It would be sooo much better if the wrong rooms with openings (all rooms are prefabs) would destroy and replace themselves. I already tried working with raycasts but I didn’t manage to fix it.
Before posting here I really wanted to solve these issues by myself, but I am at the very beginning of programming with C# and Unity so my knowledge and experience is very limited which makes it really difficult to me.
I am using the following scripts:
[RoomSpawner → spawns rooms and the box]
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 random;
public bool spawned = false;
static int maxRooms;
void Start ()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
StartCoroutine(SpawnRooms());
}
IEnumerator SpawnRooms()
{
yield return new WaitForSeconds(0.1f);
Spawn();
}
IEnumerator FixOpenRooms()
{
yield return new WaitForSeconds(1.5f);
Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
Destroy(gameObject);
Debug.Log("Fixed!");
}
void Spawn ()
{
if (spawned == false) // && maxRooms < 15)
{
if (openingDirection == 1) // Spawn Bottom Door
{
random = Random.Range(0, templates.bottomRooms.Length);
Instantiate(templates.bottomRooms[random], transform.position, Quaternion.identity);
//maxRooms++;
}
else if (openingDirection == 2) // Spawn Top Door
{
random = Random.Range(0, templates.topRooms.Length);
Instantiate(templates.topRooms[random], transform.position, Quaternion.identity);
//maxRooms++;
}
else if (openingDirection == 3) // Spawn Left Door
{
random = Random.Range(0, templates.leftRooms.Length);
Instantiate(templates.leftRooms[random], transform.position, Quaternion.identity);
//maxRooms++;
}
else if (openingDirection == 4) // Spawn Right Door
{
random = Random.Range(0, templates.rightRooms.Length);
Instantiate(templates.rightRooms[random], transform.position, Quaternion.identity);
//maxRooms++;
}
spawned = true;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("SpawnPoint"))
{
if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
{
StartCoroutine(FixOpenRooms());
}
spawned = true;
}
}
}
[RoomTemplates → Array that contains all Rooms]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomTemplates : MonoBehaviour
{
public GameObject[] bottomRooms;
public GameObject[] topRooms;
public GameObject[] leftRooms;
public GameObject[] rightRooms;
public GameObject closedRoom;
}
[Destroyer → Prevents the box from spawning at the entry room]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroyer : MonoBehaviour
{
private RoomTemplates templates;
void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag("Player") && !other.CompareTag("ClosedRoom"))
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Destroy(other.gameObject);
}
}
}
I appreciate any kind of help! Thank you!