Unity3D 獲取與設置對象Transform組件下的position,rotation

//獲取對象Transform組件下的position
float xx;
float yy;
float zz;
xx = GameObject.Find("objName").GetComponent<Transform>().position.x;
yy = GameObject.Find("objName").GetComponent<Transform>().position.y;
zz = GameObject.Find("objName").GetComponent<Transform>().position.z;

//設置對象Transform組件下的position
GameObject.Find ("objName").GetComponent<Transform>().position = new Vector3(xx,yy,zz);

//獲取對象Transform 組件下的 rotation
float rx;
float ry;
float rz;
rx = GameObject.Find ("objName").GetComponent<Transform> ().localEulerAngles.x;
ry = GameObject.Find ("objName").GetComponent<Transform> ().localEulerAngles.y;
rz = GameObject.Find ("objName").GetComponent<Transform> ().localEulerAngles.z;

//設置對象Transform組件下的 rotation 
GameObject.Find ("objName").GetComponent<Transform> ().rotation = Quaternion.Euler(rx, ry, rz);



其中postion的獲取與設置比較簡單,需要注意的是rotation的獲取  不能直接用rotation.x 獲取,這樣得到的數是一個-1到1的小數,需要用localEulerAngles.x的方法獲取

rotation的設置同樣值得注意,需要用到四元數 Quaternion.Euler(x,y,z);的方式實現。切記,切記。

發佈了88 篇原創文章 · 獲贊 46 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章