I’m trying to figure out how to instantiate a staircase using for loops and I can’t figure out the correct way to do it. The following code is just an 8x8 square, and I’ve tried everything I can think of and I still can’t get it to form a staircase.
public GameObject brick = null;
public int length = 8;
public int height = 8;
// Use this for initialization
void Start ()
{
for(int i = 0; i < length; i++)
{
for(int j = 0; j < height; j++)
{
Instantiate (brick, new Vector3(i, j, 0.0f), transform.rotation);
}
}
}
Well, essentially what you are doing right now is creating a wall 8x8. So keep in mind that a staircase not only has length and height, but it also has depth. If you want to create a staircase through code, you would need to modify the z value as well to achieve that sort of depth.
This should work (I’m using 5 as an arbitrary value, this is entirely up to you):
// Use this for initialization
void Start ()
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < length; j++)
{
for (int k = i * 5; k < (i+1) * 5; k++)
{
Instantiate (brick, new Vector3(j, i, k), Quaternion.identity);
}
}
}
}