- Home /
World Spawn Changing Y to own Block
I am using the following script to create my world with a little help I have got it set up to spawn my blocks in all directions. The problem I currently have is that I need the Y to use its own blocks.
using UnityEngine;
using System.Collections;
public class WorldSpawn : MonoBehaviour {
public GameObject block1;
public GameObject block2;
int worldWidth = 10;
int worldHeight = 10;
int worldThickness = 4;
float spawnSpeed = 0;
void Start ()
{
StartCoroutine(CreateWorld());
}
IEnumerator CreateWorld (){
for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int y =0; y<worldThickness; y+=1) {//This is the one that I need to make use a differnt block.
yield return new WaitForSeconds(spawnSpeed);//This is the one that I need to make use a differnt block.
GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
block.transform.localPosition = new Vector3(x, y, z);
}
}
}
}
}
when you say 'the Y' needs to use a different block, do you mean that you're using a different block when Y = 1 or some other number?
When it spawns the Y patter which is everything below the top layer I would like it to use a different block. Like the ground the top layer has grass on it aka my X and Z but under that you just have dirt aka my y. I need the y to use its own block so I can make it only dirt. Thanks.
Answer by creighcl · Jul 16, 2013 at 01:00 AM
Forgive me if I still misunderstood, but it seems to me that since you're looping through x, y, and z - you'll just have to check what the Y value is and Instantiate the block based on that since you're building the world a column (up/down) at a time, then a row (on the z axis) across the x axis.
So you could do something like this, assuming that the top layer is block1 (or grass) and everything below the first block is block2 (or dirt)
IEnumerator CreateWorld (){
for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int y =0; y<worldThickness; y+=1) {
yield return new WaitForSeconds(spawnSpeed);
if (y==worldThickness-1){ //when Y = the highest layer, do block1
GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
} else {
GameObject block = Instantiate(block2,block2.transform.position, block2.transform.rotation)as GameObject;
}
block.transform.localPosition = new Vector3(x, y, z);
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help converting C# to Unityscript 1 Answer
Selecting and deselecting not working 0 Answers