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()
{
}
}
我搞砸了。例如,我想建造5个楼梯,但环路超过了楼梯的大小,而不是楼梯的数量。
第二个步骤是创建10套楼梯,而不是5套楼梯:
另一个问题是,我怎样才能使每个楼梯的建造缓慢?现在只是延迟缓慢实例化了,但是如何才能延迟生成每个楼梯呢?
脚本检查器的屏幕截图:
我当前的代码:
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()
{
}
}
答案 0 :(得分:3)
根本没有理由循环楼梯的大小;您想循环访问stairsNumber
,而该代码尚未在您的代码中使用。
此外,您无需更改楼梯位置的x
组件。将其放在0f
(或任何您需要的位置)上。
您的楼梯位置的y
和z
分量(相对于起点)都应为stairHeight
的因数。在这种特殊情况下,您希望它们等于stairHeight
,以便获得“正方形”阶梯形状。
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);
}
}
如果将stairSize
更改为Vector3
,则可以直接将stairSize
用作localScale
,然后将stairsHeight
增加{{1 }},而不只是stairsSize.y
。
如果要偏移楼梯的起始位置,则需要包括一个偏移量。建议您将其与高度计数器分开,直到需要添加它们为止。
此外,如果您想使用矩形尺寸的台阶,请保持1f
乘以高度,以找出每个台阶水平移动的距离。
结合这些更改可能看起来像这样:
widthFactor