Transform類繼承自Component類,并實現了IEnumberable接口。Transform是GameObject必須擁有得一個組件,用來管理所在GameObject對象的坐標位置、選擇角度、和大小縮放。
Transform實現了IEnumberable接口,因此可以在程序中使用foreach()方法快速遍歷子物體的Transform結構。
Transfrom類實例屬性
在Transform類中,涉及的實例屬性有 eulerAngles 、forwardr 、hasChanged、localPosition、localToWorldMatrix 、parent 和 worldToLocalMatrix屬性
eulerAngles屬性:歐拉角
基本語法
public Vector3 eulerAngles{ get; set; }
功能說明 :此屬性用于返回或設置GameObject對象的歐拉角
1、在Unity引擎中使用四元數Quaternion來存儲和表示GameObject的旋轉角度,無論是在Inspector面板中對Rotation設置了數值,還是在腳本中對transform.eulerAngles賦予了數值,程序在編譯運行時都會把它們轉換成Quaternion類型再計算。
2、只能對transform.eulerAngles進行整體賦值,如transform.eulerAngles=new Vector(1.0f,2.0f,3.0f),不可以對transform.eulerAngles的單獨分量進行賦值。
3、transform.eulerAngles.x返回值的范圍為[0,90]和[270,360);transform.eulerAngles.y和transform.eulerAngles.z返回值的范圍為[0,360)。
4、對transform.eulerAngles進行賦值或獲取transform.eulerAngles的值都是相對世界坐標系而言的,若要相對transform的父物體(如果有的話)進行角度的變換則需要使用屬性localEulerAngles來設置
5、設在腳本中有代碼:transform.eulerAngles=new Vector3(10.0f, 20.0f ,30.0f),則GameObject對象會先沿著z軸旋轉30度,再沿著x軸旋轉10度,最后再沿著y軸旋轉20度。(不同的旋轉執行順序,物體的最終狀態是不同的)
forward屬性:z軸單位向量
基本語法
public Vector3 forward{get ; set;}
功能說明:此屬性用于返回或設置transform自身坐標系中z軸方向的單位向量對應的世界坐標系中的單位向量。transform.forward即為transform.TransformDirection(new Vector3(0.0f , 0.0f ,1.0f))的簡化方式。
hasChanged屬性:transform組件是否被修改
基本語法
public bool hasChanged{ get; set;}
功能說明
此屬性用于判斷GameObject對象從上次將此屬性設為false以來,其transform組件的屬性是否被修改過。
注意:即使transform某個屬性修改后的值與修改前的值相同,hasChanged的返回值仍然為true
localPosition屬性:局部坐標系位置
基本語法
public Vector3 localPosition{ get; set; }
功能說明
此屬性用于設置或返回GameObject對象在局部坐標系中的位置,若無父級對象則和屬性Transform.position返回值相同。
transform.loaclPosition的值受父級對象屬性影響,當transform.localPosition的值增加1時,transform.position值的贈量不一定是1,而是在相對父級坐標系中增加父級對象的值。
localToWorldMatrix屬性:轉換矩陣
基本語法
public Matrix4x4 localToWorldMatrix{ get; }
功能說明
此屬性用于返回從transform局部坐標系向世界坐標系轉換的Matrix4x4矩陣
parent屬性:父物體Transform實例
基本語法
public Transform parent{get;set;}
功能說明
此屬性用于返回父物體的Transform實例。
transform.parent只能返回父一級對象的Transform,若要返回父物體的父物體,可以使用transform.parent.parent,更多級父物體以此類推。
若父物體不存在,則返回null。
若想返回transform的最頂層的父物體,可以使用transform.rooot屬性
worldToLocalMatrix屬性:轉換矩陣
基本語法
public Matrix4x4 worldToLocalMatrix{ get; }
功能說明
此屬性用于返回物體從世界坐標系向transform自身坐標系轉換的Matrix4x4矩陣
Transform類實例方法
DetachChildren方法:分離物體層級關系
基本語法
public void DetachChildren();
功能說明
此方法的功能是使GameObject對象的所有子物體和自身分離層級關系,當子物體的行為不再依賴父物體時可以用此方法使父子關系分離。
若子物體仍有子物體,分離后的子物體將保留子物體的子物體的層級關系
GetChild方法:獲取GameObject對象子類
基本語法
public Transform GetChild(int index);
功能說明
此方法用于返回transform的索引值為index的子類Transform實例。
參數index的值要小于transform的childCount值
InverseTransformDirection方法:坐標系轉換
基本語法
1、public Vector3 InverseTransformDirection(Vector3 direction);
其中參數direction為待轉換的向量
2、public Vector3 InverseTransformDirection(float x,float y,float z);
功能說明:
此方法用于將參數direction從世界坐標系轉換到GameObject對象的局部坐標系。
InverseTransformPoint方法:點的相對坐標向量
基本語法
1、public Vector3 InverseTransformPoint(Vector3 position);
2、public Vector3 InverseTransformPoint(float x, float y, float z);
功能說明
此方法用于返回參數position向量相對于GameObject對象局部坐標系的差向量,即返回向量position和向量transform.position的差值。
IsChildOf方法:是否為子物體
基本語法
public bool IsChildOf(Transform parent);
其中參數parent為父物體的Transform實例
功能說明
此方法用于判斷transform對應的GameObject對象是否為參數parent的子物體
LookAt方法:物體朝向
基本語法
1、public void LookAt(Transform target);
2、public void LookAt(Vector3 worldPosition);
3、public void LookAt(Transform target,Vector3 worldUp);
4、public void LookAt(Vector3 worldPosition , Vector3 worldUp);
其中參數target為transform自身坐標系中z軸指向的目標,參數worldUp為transform自身坐標系中y軸最大限度指向的方向
功能說明
此方法的功能是使得GameObject對象自身坐標系中的z軸指向target,y軸方向最大限度地指向worldUp方向。
Rotate方法:繞坐標軸旋轉
基本語法
1、public void Rotate(Vector3 eulerAngles);
2、public void Rotate(Vector3 eulerAngles , Space relativeTo);
3、public void Rotate(float xAngle , float yAngle ,float zAngle);
4、public void Rotate(flaot xAngle , float yAngle ,float zAngle ,Space relativeTo);
其中參數eulerAngles為transform要旋轉的歐拉角,參數relativeTo為transform旋轉時參考的坐標系,默認為Space.Self。
功能說明
此方法的功能是使得transform實例在相對參數relativeTo的坐標系中旋轉歐拉角eulerAngles。
Rotate方法:繞某個向量旋轉
基本語法
1、public void Rotate(Vector3 axis ,float angle);
2、public void Rotate(Vector3 axis ,float angle ,Space relativeTo);
其中參數axis為旋轉軸方向,參數angle為旋轉角度,參數relativeTo為參考坐標系,默認為Space.self。
功能說明
此方法的功能是使得GameObject對象在relativeTo坐標系中繞軸向量axis旋轉angle度。
RotateAround方法:繞軸點旋轉
基本語法
1、public void RotateAround(Vector3 axis ,float angle);
2、public void RotateAround(Vector3 point,Vector3 axis ,float angle);
其中參數point為參考點坐標,參數axis為旋轉軸方向,參數angle為旋轉角度
功能說明
此方法的功能是使得GameObject對象繞著point點的axis方向旋轉angle度
TransformDirection方法:坐標系轉換
基本語法
1、public Vector3 TransformDirection(Vector3 direction);
其中參數direction為待轉換的Vector3實例向量。
2、public Vector3 TransformDirection(float x ,float y ,float z);
功能說明
此方法用于將向量direction從transform局部坐標系轉換到世界坐標系
TransformPoint方法:點的世界坐標位置
基本語法
1、public Vector3 TransformPoint(Vector3 position);
其中參數position為transform局部坐標系的向量
2、public Vector3 TransformPoint(float x,float y,float z);
功能說明
此方法用于返回GameObject對象局部坐標系中向量position在世界坐標系中的位置
Translate方法:相對坐標系移動
基本語法
1、public void Translate(Vector3 translation);
2、public void Translate(Vector3 translation ,Space relativeTo);
3、public void Translate(float x ,float y , float z);
4、public void Translate(float x ,float y , float z , Space relativeTo);
其中參數transform為移動向量,包括方向和大小,參數relativeTo為參考坐標系空間,默認為Space.Self
功能說明
此方法的功能是使得GameObject對象在參數relativeTo的坐標系空間中移動參數transform指定的向量
Translate方法:相對其他物體移動
基本語法
1、public void Translate(Vector3 translation , Transform relativeTo);
2、public void Translate(float x ,float y,float z, Transform relativeTo);
其中參數translation為移動向量,包括方向和大小,參數relativeTo為移動參考物體,默認為Space.World。
功能說明
此方法的功能是使得GameObject對象在相對relativeTo的坐標系中移動向量translation。