1

我有几个预制件用于随机生成“房间”,但相同的代码导致宽度不一致:

奇数宽度

顶部(northWall)应该与底部(southWall)的宽度相同,但它显然是大小的 3 倍。

这是实例化其他“墙”预制件的“房间”预制件(此时它们基本上都只是四边形)。

public float length;
public float width;
public float wallDepth;

public Transform northWall;
public Transform southWall;

void Start () {
    length          = Random.Range (5.0f, 25.0f);
    width           = Random.Range (5.0f, 25.0f);
    wallDepth       = 2.0f;

    transform.localScale = new Vector3 (width, length, 0.0f);

    Instantiate (northWall, new Vector3(transform.localPosition.x, transform.localPosition.y + (transform.localScale.y / 2) + (wallDepth / 2), 10.0f), Quaternion.identity);
    northWall.transform.localScale = new Vector3 (width, wallDepth, 10.0f);

    Instantiate (southWall, new Vector3(transform.localPosition.x, transform.localPosition.y - (transform.localScale.y / 2) - (wallDepth / 2), 10.0f), Quaternion.identity);
    southWall.transform.localScale = new Vector3 (width, wallDepth, 10.0f);

}

我要疯了吗?是不是太晚了,我错过了什么?

谢谢。

4

1 回答 1

0

我已经解决了这个问题。我认为这是因为我为四面墙中的每一面都使用了相同的预制件。我创建了一个函数,它返回预制件的克隆并将其重新分配给原始四边形。这样,比例和位置变换仅适用于四边形的复制版本:

GameObject buildWall (GameObject source, float width, float length, float x, float y, float z) {
    GameObject clone = Instantiate (source) as GameObject;

        clone.transform.localScale = new Vector3 (width, length, 0.0f);
        clone.transform.localPosition = new Vector3 (x, y, z);

    return clone;
}

实例化现在看起来像这样:

northWall = buildWall (northWall, width, wallDepth, transform.localPosition.x, transform.localPosition.y + (transform.localScale.y / 2) + (wallDepth / 2), 10.0f);

我不知道这是否是最有效(甚至是正确)的方法,但它现在似乎有效。

于 2014-12-30T23:14:24.090 回答