//獲取對象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);的方式實現。切記,切記。