- Home /
[2D] Random Terrain Generator
So I've been working on a script for a 2D Terrain generator. It isn't one for hills but instead the game is supposed to be inside and I'm having trouble spawning the different floors. The issue is I don't know how to do the inverting the of the code properly. (Also if anyone knows of a better way of doing the entire this please tell me). It spawns just no the way I need it to
void spawnHigherFloors(){
GameObject stairClone = null;
GameObject floor = null;
if(spawning){
for(int i = 0; i < sizeY -1; i++){
int stairTemp;
int stairTemp1;
int stairPos;
int posY;
//Get random position to spawn the stairs
if(i == 0){
stairTemp = Random.Range(1, sizeX -1);
stairPos = stairTemp * 6;
posY = i * 6;
}else{
stairTemp = Random.Range(1, sizeX -1);
stairTemp1 = sizeX - stairTemp;
stairPos = stairTemp1 * 6;
posY = i * 6;
}
if(i % 2 == 0){
/*-------Spawn Even numbered floor--------*/
//Spawn the staircase
stairClone = (GameObject) Instantiate(staircase, new Vector3(stairPos, posY, 0), Quaternion.identity);
stairClone.transform.parent = this.transform;
int forMath = sizeX -stairTemp -1;
//Spawn rows of other floors
for(int j = 0; j < forMath; j++){
if(j < forMath -1){
float stairX = stairClone.transform.position.x + 8.5f;
floor = (GameObject) Instantiate(secondFloor);
floor.transform.position = new Vector2(stairX + j * 6, posY + 6);
}else if(j == forMath -1){
float stairX = stairClone.transform.position.x + 8.5f;
floor = (GameObject) Instantiate(endFloor);
floor.transform.position = new Vector2(stairX + j * 6, posY + 6);
}
floor.transform.parent = this.transform;
}
}else{
/*-------Spawn Odd numbered floor--------*/
stairClone = (GameObject) Instantiate(staircase, new Vector3(stairPos, posY, 0), Quaternion.identity);
Vector3 theScale = stairClone.transform.localScale;
theScale.x *= -1;
stairClone.transform.localScale = theScale;
stairClone.transform.parent = this.transform;
int forMath = sizeX -stairTemp -1;
for(int j = 0; j < forMath; j++){
if(j < forMath -1){
float stairX = stairClone.transform.position.x - 8.5f;
floor = (GameObject) Instantiate(secondFloor);
floor.transform.position = new Vector2(stairX - j * 6, posY + 6);
}else if(j == forMath -1){
float stairX = stairClone.transform.position.x - 8.5f;
floor = (GameObject) Instantiate(endFloor);
floor.transform.position = new Vector2(stairX - j * 6, posY + 6);
}
floor.transform.parent = this.transform;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How to Spawn an Object in between Randomly Spawned Objects with no Overlapping? 0 Answers
Can't get game objects to instantiate at random positions without overlap - please help! 3 Answers
Is it possible to use scripts without MonoBehaviour on GameObjects? 1 Answer
Is the map of this game fixed or randomly generated? 1 Answer
2d spawn across screen 1 Answer