How can I generate stairs?












2















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()
{

}
}









share|improve this question




















  • 1





    Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

    – Vitor Figueredo
    Nov 15 '18 at 18:30






  • 1





    @VitorFigueredo Added it to my question.

    – Dubi Duboni
    Nov 15 '18 at 19:08
















2















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()
{

}
}









share|improve this question




















  • 1





    Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

    – Vitor Figueredo
    Nov 15 '18 at 18:30






  • 1





    @VitorFigueredo Added it to my question.

    – Dubi Duboni
    Nov 15 '18 at 19:08














2












2








2








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()
{

}
}









share|improve this question
















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()
{

}
}






c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:15







Dubi Duboni

















asked Nov 15 '18 at 17:46









Dubi DuboniDubi Duboni

392110




392110








  • 1





    Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

    – Vitor Figueredo
    Nov 15 '18 at 18:30






  • 1





    @VitorFigueredo Added it to my question.

    – Dubi Duboni
    Nov 15 '18 at 19:08














  • 1





    Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

    – Vitor Figueredo
    Nov 15 '18 at 18:30






  • 1





    @VitorFigueredo Added it to my question.

    – Dubi Duboni
    Nov 15 '18 at 19:08








1




1





Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

– Vitor Figueredo
Nov 15 '18 at 18:30





Can you post a screenshot of your script inspector on Unity? I'd like to reproduce the situation.

– Vitor Figueredo
Nov 15 '18 at 18:30




1




1





@VitorFigueredo Added it to my question.

– Dubi Duboni
Nov 15 '18 at 19:08





@VitorFigueredo Added it to my question.

– Dubi Duboni
Nov 15 '18 at 19:08












1 Answer
1






active

oldest

votes


















3














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);
}
}





share|improve this answer


























  • Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

    – Dubi Duboni
    Nov 15 '18 at 21:14











  • have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

    – Dubi Duboni
    Nov 15 '18 at 21:17











  • @DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

    – Ruzihm
    Nov 15 '18 at 21:38








  • 1





    If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

    – Ruzihm
    Nov 15 '18 at 22:02






  • 1





    stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

    – Ruzihm
    Nov 15 '18 at 22:19













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325204%2fhow-can-i-generate-stairs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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);
}
}





share|improve this answer


























  • Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

    – Dubi Duboni
    Nov 15 '18 at 21:14











  • have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

    – Dubi Duboni
    Nov 15 '18 at 21:17











  • @DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

    – Ruzihm
    Nov 15 '18 at 21:38








  • 1





    If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

    – Ruzihm
    Nov 15 '18 at 22:02






  • 1





    stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

    – Ruzihm
    Nov 15 '18 at 22:19


















3














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);
}
}





share|improve this answer


























  • Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

    – Dubi Duboni
    Nov 15 '18 at 21:14











  • have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

    – Dubi Duboni
    Nov 15 '18 at 21:17











  • @DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

    – Ruzihm
    Nov 15 '18 at 21:38








  • 1





    If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

    – Ruzihm
    Nov 15 '18 at 22:02






  • 1





    stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

    – Ruzihm
    Nov 15 '18 at 22:19
















3












3








3







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);
}
}





share|improve this answer















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);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 21:47

























answered Nov 15 '18 at 20:21









RuzihmRuzihm

3,71611628




3,71611628













  • Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

    – Dubi Duboni
    Nov 15 '18 at 21:14











  • have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

    – Dubi Duboni
    Nov 15 '18 at 21:17











  • @DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

    – Ruzihm
    Nov 15 '18 at 21:38








  • 1





    If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

    – Ruzihm
    Nov 15 '18 at 22:02






  • 1





    stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

    – Ruzihm
    Nov 15 '18 at 22:19





















  • Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

    – Dubi Duboni
    Nov 15 '18 at 21:14











  • have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

    – Dubi Duboni
    Nov 15 '18 at 21:17











  • @DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

    – Ruzihm
    Nov 15 '18 at 21:38








  • 1





    If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

    – Ruzihm
    Nov 15 '18 at 22:02






  • 1





    stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

    – Ruzihm
    Nov 15 '18 at 22:19



















Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

– Dubi Duboni
Nov 15 '18 at 21:14





Working great. I tried now for some minutes to use also the position x now it's set to 0f and also the scale is now set to 1f. What if I want to create bigger stairs on the height scale ? And if I want to move the stairs on also z position ? And this: stairsStartPositionHeight += 1; if I want to start the stairs in the sky ? It's all working but too many variables and almost same names.

– Dubi Duboni
Nov 15 '18 at 21:14













have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

– Dubi Duboni
Nov 15 '18 at 21:17





have updated my question with my code now. Seems to be a bit messed. I think this variable stairsStartPositionHeight and this stairsScalingHaight variable should be part of the vectors. Instead vector2 to change it to vector3 but not sure how to make the code to work with vector3 for position and scaling.

– Dubi Duboni
Nov 15 '18 at 21:17













@DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

– Ruzihm
Nov 15 '18 at 21:38







@DubiDuboni I edited my answer to include how you could start your stairs from a different position, as well as have different step lengths compared to their height.

– Ruzihm
Nov 15 '18 at 21:38






1




1





If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

– Ruzihm
Nov 15 '18 at 22:02





If you don't want gaps, you need to change stairSize.z to be larger. When stairSize.z < stairSize.y * stepWidthFactor then there are gaps.

– Ruzihm
Nov 15 '18 at 22:02




1




1





stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

– Ruzihm
Nov 15 '18 at 22:19







stairsHeight is different than stairsStartPosition. stairsHeight keeps track of how high the last stair you've made so far is. They are only equal for the 0 step. You could get rid of stairsHeight += stairsSize.y; and replace every remaining mention of stairsHeight with (i * stairsSize.y) and that would still work.

– Ruzihm
Nov 15 '18 at 22:19






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325204%2fhow-can-i-generate-stairs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python