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


//獲取對象Transform組件下的position
float
xx; float yy; float zz; /** * 初始化保留整體模型位置,用於還原模型位置(Position) * transform.position 獲取世界位置 * transform.localPosition 獲取本地位置 **/ 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);

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

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

//獲取對象Transform 組件下的 rotation
float rx;  float ry;  float rz;
/**
 * 初始化保留整體模型角度,用於還原模型角度(Rotation)
 * transform.eulerAngles 獲取世界模型角度
 * transform.localEulerAngles 獲取本地模型角度
 **/
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);

   參考文章:https://blog.csdn.net/weiming8517/article/details/50960918?utm_source=blogxgwz7


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM