2014-12-30 27 views
1

不一致浮子我有我使用隨機生成一個「房間」幾個預製件,但相同的代碼導致不一致的寬度:使用Unity

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

} 

我會瘋了嗎?它剛剛晚了,我錯過了什麼?

謝謝。

+0

你有什麼設置北牆和南牆在檢查員? – Lefty

+0

我剛剛將一個Wall預製件拖放到它們中的每一個上。 Wall是一個帶有箱式對撞機的四輪車。 – stefannew

+0

那麼檢查員內部的規格是什麼? –

回答

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

我不知道這是否是最有效的(或者甚至是正確的)的方式去做這件事,但它似乎現在工作。