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

問題を解決しました。4つの壁それぞれに同じプレハブを使用していたことが原因だと思います。プレハブのクローンを返し、それを元のクワッドに再割り当てする関数を作成しました。このように、スケールと位置の変換は、クワッドのコピーされたバージョンにのみ適用されます。

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 に答える