Question by
VoidSeekerSlayer · Nov 20, 2019 at 10:43 AM ·
c#unity 5
Random Generation (Dungeon) with improvements in mind.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Generator : MonoBehaviour {
//This creates slots for the prefabs to be stored and used. public GameObject[] Room5;
public GameObject[] Room10;
public GameObject[] Room15;
public GameObject xRoad;
public GameObject Spawner;
// Iterations are statements to be done the amount times you told it it ranges from 0 and up.
public int minIterations;
public int maxIterations;
int Iterations;
// Start is called before the first frame update
void Start()
{
Iterations = Random.Range(minIterations,maxIterations);
StartCoroutine(SpawnRooms());
}
// Update is called once per frame
IEnumerator SpawnRooms(){
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 2.5f);
for (int i = 0; i < Iterations; i++)
{
int RoomChoice = Random.Range (0,4);
if (RoomChoice == 0){ //5x5
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 2.5f);
Instantiate (Room5[Random.Range(0,Room5.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 2.5f);
}
if (RoomChoice == 1){ //10x10
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 5f);
Instantiate (Room10[Random.Range(0,Room10.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 5f);
}
if (RoomChoice == 2){ //15x15
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 7.5f);
Instantiate (Room15[Random.Range(0,Room15.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 7.5f);
}
if (RoomChoice == 3) {
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 7.5f);
Instantiate (xRoad, Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 7.5f, Spawner.transform.position.y, Spawner.transform.position.z );
Iterations = Random.Range(minIterations,maxIterations);
for (int j = 0; j < Iterations; j++)
{
int RoomChoice2 = Random.Range (0,3);
if (RoomChoice2 == 0){ //5x5
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 2.5f, Spawner.transform.position.y, Spawner.transform.position.z );
Instantiate (Room5[Random.Range(0,Room5.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 2.5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
if (RoomChoice2 == 1){ //10x10
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 5f, Spawner.transform.position.y, Spawner.transform.position.z);
Instantiate (Room10[Random.Range(0,Room10.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
if (RoomChoice2 == 2){ //15x15
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 7.5f, Spawner.transform.position.y, Spawner.transform.position.z );
Instantiate (Room15[Random.Range(0,Room15.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x + 7.5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
yield return new WaitForSeconds(0.1f);
}
Spawner.transform.position = new Vector3 (-7.5f, Spawner.transform.position.y, Spawner.transform.position.z);
Iterations = Random.Range(minIterations,maxIterations);
for (int k= 0; k < Iterations; k++)
{
int RoomChoice2 = Random.Range (0,3);
if (RoomChoice2 == 0){ //5x5
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 2.5f, Spawner.transform.position.y, Spawner.transform.position.z );
Instantiate (Room5[Random.Range(0,Room5.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 2.5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
if (RoomChoice2 == 1){ //10x10
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 5f, Spawner.transform.position.y, Spawner.transform.position.z);
Instantiate (Room10[Random.Range(0,Room10.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
if (RoomChoice2 == 2){ //15x15
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 7.5f, Spawner.transform.position.y, Spawner.transform.position.z );
Instantiate (Room15[Random.Range(0,Room15.Length)], Spawner.transform.position, Spawner.transform.rotation);
Spawner.transform.position = new Vector3 (Spawner.transform.position.x - 7.5f, Spawner.transform.position.y, Spawner.transform.position.z );
}
yield return new WaitForSeconds(0.1f);
}
Spawner.transform.position = new Vector3 (0, Spawner.transform.position.y, Spawner.transform.position.z + 7.5f);
}
yield return new WaitForSeconds(0.1f);
}
}
}
As you can see from the picture this is the generator that randomly generates a dungeon but I would like it to generate more on the X and Y crossing each more more frequent and as an extra task I would like it to go up. Prefabs (cubes) are being used all to scale 5x5 + 10x10 + 15x15 (all duplicated 3x) in each of the rooms as you can see in this script. 5x5 and the duplicates going to room5 and 10x10 going to room10 same with 5s. In short how do I improve on this generator?
rg.png
(52.2 kB)
Comment