gpt4 book ai didi

c# - 我怎样才能生成楼梯?

转载 作者:行者123 更新时间:2023-11-30 21:31:23 26 4
gpt4 key购买 nike

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

}
}

我搞砸了。例如,我想 build 5 个楼梯,但循环超过了楼梯的大小,而不是楼梯的数量。

其次,它创建了 10 组楼梯而不是 5 组楼梯:

stairs

另一个问题是我怎样才能让每个楼梯都慢慢 build ?现在它只是缓慢地延迟实例化,但我如何延迟生成每个楼梯?

脚本检查器的屏幕截图:

Inspector

我当前的代码:

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

}
}

最佳答案

根本没有理由遍历楼梯的大小;您想要遍历 stairsNumber,它在您的代码中尚未使用。

此外,您不需要更改楼梯位置的 x 组件。将其保持在 0f(或您需要的任何值)。

楼梯位置的 yz 分量(相对于起点)都应该是 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 增加 stairsSize.y 而不是仅仅增加 1f

如果您想偏移楼梯的起始位置,则需要包含一个偏移量。我建议将它与高度计数器分开,直到您需要添加它们。

此外,如果您想要矩形大小的步长,请将 widthFactor 乘以高度以计算每个步长水平移动的距离。

组合这些更改可能如下所示:

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

关于c# - 我怎样才能生成楼梯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com