与Unity不一致的花车

时间:2014-12-30 07:01:44

标签: unity3d unity3d-2dtools

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

Odd widths

顶部(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);

}

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

感谢。

1 个答案:

答案 0 :(得分:0)

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

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

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