How can I generate stairs?

Viewed 289
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateStairs : MonoBehaviour
{
    public GameObject stairsPrefab;
    public int delay = 3;
    public int stairsNumber = 5;
    public int stairsHeight = 0;
    public Vector3 stairsPosition;
    public Vector2 stairsSize;

    // Use this for initialization
    void Start ()
    {
        StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update ()
    {

    }

    private IEnumerator BuildStairs()
    {
        for (float i = 0; i <= stairsSize.x; i++)
        {
            for (float k = 0; k <= stairsSize.y; k++)
            {
                stairsPosition = new Vector3(i, stairsHeight, k);
                GameObject stairs = Instantiate(stairsPrefab, stairsPosition, Quaternion.identity);
                stairs.transform.localScale = new Vector3(stairsSize.x, 1 , stairsSize.y);

                stairsHeight += 1;

                yield return new WaitForSeconds(delay);
            }
        }
    }

    private void CalculateNextStair()
    {

    }
}

I messed it up. For example I want to build 5 stairs but the loops are over the stairs size and not number of stairs.

Second it's creating 10 sets of stairs not 5 stairs:

stairs

Another problem is how can I make that each stair will be build slowly ? Now it's just Instantiate slowly with delay but how can I generate each stair with delay?

Screenshot of the script inspector:

Inspector

My current code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateStairs : MonoBehaviour
{
    public GameObject stairsPrefab;
    public float delay = 0.3f;
    public int stairsNumber = 5;
    public int stairsPositions = 0;
    public int stairsStartPositionHeight = 0;
    public float stairsScalingHaight = 1;
    public Vector2 stairsPosition;
    public Vector2 stairsSize;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator BuildStairs()
    {
        for (float i = 0; i <= stairsNumber; i++)
        {
            // x=0f, y=z=stairsHeight
            stairsPosition = new Vector3(0f, stairsPositions, stairsPositions);
            GameObject stairs = Instantiate(
                    stairsPrefab,
                    stairsPosition,
                    Quaternion.identity);
            stairs.transform.localScale = new Vector3(
                    stairsSize.x,
                    stairsScalingHaight,
                    stairsSize.y);

            stairsStartPositionHeight += 1;

            yield return new WaitForSeconds(delay);
        }
    }

    private void CalculateNextStair()
    {

    }
}
1 Answers

There's no reason to loop over the size of the stairs at all; you want to loop over stairsNumber, which is yet unused in your code.

Also, you don't need to change the x component of your stairs' positions. Keep it at 0f (or whatever you need).

The y and z components of your stairs positions (relative to the starting point) should both be factors of stairHeight. In this particular case, you want them to be equal to stairHeight, so that you get "square" step shapes.

private IEnumerator BuildStairs()
{
    for (int i = 0; i <= stairsNumber ; i++) {
        // x=0f, y=z=stairsHeight
        stairsPosition = new Vector3(0f, stairsHeight, stairsHeight);
        GameObject stairs = Instantiate(
                stairsPrefab, 
                stairsPosition, 
                Quaternion.identity);
        stairs.transform.localScale = new Vector3(
                stairsSize.x, 
                1f , 
                stairsSize.y);

        stairsHeight += 1f;

        yield return new WaitForSeconds(delay);
    }
}

If you change stairSize to be a Vector3, then you can just use stairSize directly as the localScale, and increment stairsHeight by stairsSize.y instead of just 1f.

If you want to offset the starting position of your stairs, you need to include an offset. I recommend keeping it separate from the height counter until you need to add them.

Also, if you want to have rectangular sized steps, keep a widthFactor to multiply by the height to find how far each step moves horizontally.

Combining these changes might look like this:

Vector3 stairSize;
float stepWidthFactor=1f;
Vector3 stairsStartPosition;

private IEnumerator BuildStairs() {
    for (int i = 0; i <= stairsNumber ; i++) {

        stairsPosition = new Vector3(
                stairsStartPosition.x, 
                stairsStartPosition.y + stairsHeight,
                stairsStartPosition.z + stairsHeight*stepWidthFactor);

        GameObject stairs = Instantiate(
                stairsPrefab, 
                stairsPosition, 
                Quaternion.identity);

        stairsHeight += stairsSize.y;
        stairs.transform.localScale = stairSize;

        yield return new WaitForSeconds(delay);
    }
}
Related