Question by
VoidSeekerSlayer · May 26, 2020 at 03:50 PM ·
c#beginnergameobjectsgeneration
How to spawn in the end room randomly?
Im trying to spawn in the end room randomly, and not in the same point (in this instance its always spawning at the end walking straight forward Im trying to add a challenge and this is for me to better understand this sort of thing better if you could comment on what you do/did) Im using prefabs, just simple "rooms" for example as you see in the code its 5 by 5 etc
{
//This creates slots for the prefabs to be stored and used. This is an array.
public GameObject[] Room5;
public GameObject[] Room10;
public GameObject[] Room15;
public GameObject EndRoom;
public GameObject xRoad;
public GameObject Spawner;
// Iterations are statements that are done the amount times you told it - it ranges from 0 and up.
// For example public int minIterations = 6; this will be a set amount.
public int minIterations;
public int maxIterations;
int Iterations;
void Start()
{
//calling the method spawning in the objects running the lines of the "IEnumerator" line by line until its finished. Giving us control by using an coroutine.
Iterations = Random.Range(minIterations,maxIterations);
StartCoroutine(SpawnRooms());
}
IEnumerator SpawnRooms(){
//this is transforming the position, scale & rotation - a vector3 this is everying on the axis XYZ for a object being placed.
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 2.5f);
//this controls the numbers as an whole number or decimels. Its a for loop
for (int i = 0; i < Iterations; i++)
{
//this is randomly choosing from the "room choices" the count of rooms (0, 4); --> to randomly spawn in the objects.
int RoomChoice = Random.Range (0, 4);
//this is spawning in everything going forward and how far apart the prefabs are being spawned next to each other. + 2.5f etc
//its instantiating the prefab choosing a random range to spawn across on the axis.
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);
//this is doing the same however this is spawning everything and branching off from the the "main road" this is the positive.
//Moving on the Z axis
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 );
}
}
//same as I said before just on the negative z axis.
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 );
}
}
Spawner.transform.position = new Vector3 (0, Spawner.transform.position.y, Spawner.transform.position.z + 7.5f);
}
//the delay it spawns everything in
yield return new WaitForSeconds(0);
}
//Once the generator finishes it spawns in the endroom at the end of level.
Spawner.transform.position = new Vector3 (Spawner.transform.position.x, Spawner.transform.position.y, Spawner.transform.position.z + 2.5f);
Instantiate (EndRoom, Spawner.transform.position, Spawner.transform.rotation);
}
}
Comment