[英]Unity 3D, transform.position not working on instatiate object
我創建了一個簡單的腳本來實例化一個多維數據集。 我需要知道它的位置(x 和 z)。 但是,當腳本實例化立方體時,它的 x 和 z 位置為 0。我做錯了什么?
實例化腳本示例:
public GameObject cube;
float xPos, zPos;
int randomPos;
void Start()
{
randomPos = Random.Range(10,20);
Instantiate(cube, new Vector3(1,0,randomPos),Quaternion.identity);
xPos = cube.transform.position.x;
zPos = cube.transform.position.z;
}
這是一個例子,我需要知道“cube.transform.position.x and z”但輸出總是零謝謝大家
您必須獲得的不是立方體的位置,而是來自Instantiate
創建的新立方體的位置。 Instantiate
克隆對象原始並返回克隆。
void Start(){
randomPos = Random.Range(10,20);
var newCube = Instantiate(cube, new Vector3(1,0,randomPos),Quaternion.identity);
xPos = newCube.transform.position.x;
zPos = newCube.transform.position.z;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.