I got the first room of my procedural dungeon working, or rather one room, however I dont know where to go from here to create multiple rooms as I cant just use CreateRoom again for some reason. If anyone could help I would appreciate it very much, Im new to coding so it's hard for me to understand why I cant use the same function while changing the starting position.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProcGen : MonoBehaviour
{
public GameObject[] floorTiles;
public GameObject[] wallTiles;
public float ceilingHeight;
public Vector3 roomSpawn = new Vector2(0, 0);
// Start is called before the first frame update
void Start()
{
//Instantiate(floorTiles[0]);
CreateRoom(Random.Range(2,7), Random.Range(2,9), roomSpawn);
//CreateRoom(2,2, new Vector2(50, 50));
}
// Update is called once per frame
void Update()
{
}
void CreateRoom(int width, int length, Vector2 position)
{
// FLOOR and Roof GENERATION
int tileWidth = (int)floorTiles[0].GetComponent<MeshRenderer>().bounds.size.x;
// truncate the position that was passed in
Vector2 firstPos = new Vector2(Mathf.Floor(position.x), Mathf.Floor(position.y));
Vector2 tilePos = firstPos;
for(int i = 0; i < width; i++)
{
for(int j = 0; j < length; j++)
{
Instantiate(floorTiles[Random.Range(0, floorTiles.Length)], new Vector3(tilePos.x, 0, tilePos.y), Quaternion.identity);
Instantiate(floorTiles[0], new Vector3(tilePos.x, ceilingHeight, tilePos.y), Quaternion.Euler (0f, 0f, 180f));
tilePos += new Vector2(tileWidth, 0);
}
tilePos = new Vector2(firstPos.x, tilePos.y);
tilePos += new Vector2(0, tileWidth);
}
// WALL GENERATION
float rotation = 90;
Vector2 wallPos = firstPos;
for(int i = 0; i < length; i++)
{
Instantiate(wallTiles[Random.Range(0, wallTiles.Length)], new Vector3(wallPos.x, 0, 0), Quaternion.Euler(new Vector3(0, rotation, 0)));
wallPos += new Vector2(tileWidth, 0);
}
rotation += 90;
for(int i = 0; i < width; i++)
{
Instantiate(wallTiles[Random.Range(0, wallTiles.Length)], new Vector3(0, 0, wallPos.y), Quaternion.Euler(new Vector3(0, rotation, 0)));
wallPos += new Vector2(0, tileWidth);
}
rotation += 90;
for(int i = 0; i < length; i++)
{
Instantiate(wallTiles[Random.Range(0, wallTiles.Length)], new Vector3(i * tileWidth, 0, firstPos.y + ((width - 1) * tileWidth)), Quaternion.Euler(new Vector3(0, rotation, 0)));
wallPos += new Vector2(tileWidth, 0);
}
rotation += 90;
for(int i = 0; i < width; i++)
{
Instantiate(wallTiles[Random.Range(0, wallTiles.Length)], new Vector3((length - 1) * tileWidth, 0, i * tileWidth), Quaternion.Euler(new Vector3(0, rotation, 0)));
wallPos += new Vector2(0, tileWidth);
}
}
int CalculateAddition(int left, int right)
{
return left + right;
}
}
Like I said I tried to use CreateRoom again and it overlayed it on top of spawn again, but the walls didnt correctly place like I wanted them to, they seem to be placing at the spawn again.