Unity學習——變換(Transform)組件


介紹

變換(Transform)組件確定場景中每個對象的“位置(Position)”、“旋轉(Rotation)”和“縮放(Scale)”。

每一個游戲對象(GameObject)都有一個變換(Transform)組件。

屬性

位置:變換組件在X、Y、Z軸上的位置(后面將解釋為什么不說是物體的軸上的位置信息)

旋轉:變換組件繞X、Y、Z軸所旋轉的度數

縮放:變換組件沿X、Y、Z軸上的縮放,值為1時則為原始大小(被導入或創建時物體的大小)

注意:變換的位置、旋轉以及縮放是相對於該物體的(變換組件的)父級測量的。當沒有父級時則在世界坐標下顯示其屬性。

相關腳本

(注:代碼塊上方是屬性或方法的作用域、數據類型和訪問權限(get/set),代碼塊中是具體的使用方法)

屬性

public int childCount { get; }

transform.childCount 

物體的(變換組件的)子對象的數目(不包括自身)


public Vector3 eulerAngles { get; set; }

transform.eulerAngles 

物體旋轉屬性的以度為單位的歐拉角(即與變換組件中旋轉的三個量相對應)


public Vector3 forward { get; set; }

transform.forward 

世界空間中,物體的(變換組件的)z軸

應當注意當物體旋轉角度后,其自身的坐標軸也隨之變換,使用“transform.forward”可以獲取物體當前自身坐標系(Local而非Global)的z軸


public bool hasChanged { get; set; }

transform.hasChanged

自從上一次"hasChanged"被設為false后,變換組件是否發生了變化?

應注意其默認值為true。因此需手動設置為false后進行檢測。當其改變為true后,其真值不會自身改變為false


public int hierarchyCapacity { get; set; }

transform.hierarchyCapacity

變換在層級中數據結構的變換能力

即目前該物體在Hierarchy窗口中對應的樹的可容納最大結點數

應注意其數值會隨着結點數的增加而擴容,因此對於需要頻繁增加子物體的物體,可以事先設置該值比預計的最大值高一點,以避免反復修改此值造成的性能降低


public int hierarchyCount { get; }

transform.hierarchyCount 

變化在層級中數據結構的所包含的變換組件數目

即物體及其子物體所包含的變換組件的總數目


public Vector3 localEulerAngles { get; set; }

transform.localEulerAngles

物體的相對於父級所旋轉的歐拉角

如需設置請注意使用正值且不要超過360度


public Vector3 localPosition { get; set; }

transform.localPosition 

物體相對於父級的位置,沒有父級則與世界坐標下一致


public Quaternion localRotation { get; set; }

transform.localRotation 

物體相對於父級的旋轉屬性


public Vector3 localScale { get; set; }

transform.localScale 

物體相對於父級的縮放屬性


public Matrix4x4 localToWorldMatrix { get; }

transform.localToWorldMatrix

將點從局部空間轉換為世界空間的矩陣


public Vector3 lossyScale { get; }

transform.lossyScale

世界坐標系下物體的縮放屬性

注意當物體的父級進行了縮放並且該物體旋轉后,該值不再准確


public Transform parent { get; set; }

transform.parent 

物體的父級的變換屬性


public Vector3 position { get; set; }

transform.position

物體在世界坐標系下的位置屬性


public Vector3 right { get; set; }

transform.right

世界空間中,物體的(變換組件的)x軸

應當注意當物體旋轉角度后,其自身的坐標軸也隨之變換,使用“transform.right”可以獲取物體當前自身坐標系(Local而非Global)的x軸


public Transform root { get; }

transform.root 

返回物體所在層級結構的根的變換屬性,沒有父級則為自身


public Quaternion rotation { get; set; }

transform.rotation

物體在世界坐標系下的變換屬性,以四元數形式存儲

注意它並不與變換組件中對應的旋轉的三個屬性對應(可以使用transform.eulerAngles進行修改)

它的值小於180度


public Vector3 up { get; set; }

transform.up

世界空間中,物體的(變換組件的)y軸

應當注意當物體旋轉角度后,其自身的坐標軸也隨之變換,使用“transform.right”可以獲取物體當前自身坐標系(Local而非Global)的y軸


public Matrix4x4 worldToLocalMatrix { get; }

transform.worldToLocalMatrix 

將點從世界空間轉換為局部空間的矩陣

 

 

公開方法

public void DetachChildren();

transform.DetachChildren() 

將物體的所有子對象都與其取消父子關系


public Transform Find(string n);

參數

string n:要找到的子物體的名字

返回值

如果找到了該孩子,則返回其變換組件,否則返回null

transform.Find("ChildName"); transform.Find("ChildName/GrandChildName"); 

通過名字n找到物體的子對象的變換組件

注意以上用法只適用於子物體而不適用與孫物體或更多層級下的物體查找

如需查找更低層級的物體,可以像路徑名一樣進行查找,如第二行所示


public Transform GetChild(int index);

參數

int index:子物體對應的索引

返回值

索引對應的子物體的變換組件

transform.GetChild(0);//找到第一個子物體變換組件 transform.GetChild(0).GetChild(0);//找到第一個子物體的第一個子物體的變換組件 

子物體的索引在層級窗口從上至下由0開始遞增


public int GetSiblingIndex();

返回值

子物體相對於父物體的索引值

transform.GetSiblingIndex(); 

獲取物體在層級窗口下的索引,即是其父物體的從上之下的第幾個物體(從0開始)

注意無父物體時,則會在層級窗口最外層從上至下由0開始排列


public Vector3 InverseTransformDirection(Vector3 direction);

參數

Vector3 direction:表示某個方向的三維向量

返回值

物體自身坐標系下direction的方向向量

transform.InverseTransformDirection(Vector3.forward);//將世界坐標系下的z軸方向轉換為物體自身坐標系下的方向 

將direction由世界坐標系轉換為物體的自身坐標系下的向量表示

 

public Vector3 InverseTransformDirection(float x, float y, float z);

參數

float x, float y, float z:表示方向的三個軸上的參數

返回值

物體自身坐標系下(x,y,z)的方向向量

transform.InverseTransformDirection(0, 0, 1); 

將(x,y,z)由世界坐標系轉換為物體的自身坐標系下的向量表示


public Vector3 InverseTransformPoint(Vector3 position);

參數

Vector3 position:表示某個點(位置)的三維向量

返回值

物體自身坐標系下position的位置向量

transform.InverseTransformPoint(Camera.main.transform.position);//將世界坐標系下的主相機的位置轉換為物體自身坐標系下的位置(即與相機的相對位置) 

將position由世界坐標系轉換為物體的自身坐標系下的向量表示

 

public Vector3 InverseTransformPoint(float x, float y, float z);

參數

float x, float y, float z:表示某個點(位置)的三個軸上的參數

返回值

物體自身坐標系下(x,y,z)的位置向量

transform.InverseTransformPoint((0,0,0)); 

將(x,y,z)由世界坐標系轉換為物體的自身坐標系下的向量表示

 

注意這個函數會受到縮放的影響


 

public Vector3 InverseTransformVector(Vector3 vector);

參數

Vector3 vector:某個向量

返回值

物體自身坐標系下某個向量的表示

transform.InverseTransformVector(Vector3.forward);//將世界坐標系下的z軸方向轉換為物體自身坐標系下的方向 

vector由世界坐標系轉換為物體的自身坐標系下的向量

public Vector3 InverseTransformVector(float x, float y, float z);

參數

float x, float y, float z:表示某個向量的三個軸上的參數

返回值

物體自身坐標系下(x,y,z)的轉換后的向量

transform.InverseTransformVector((0,0,0)); 

將(x,y,z)由世界坐標系轉換為物體的自身坐標系下的向量

注意這個函數會受到縮放的影響


public bool IsChildOf([NotNull] Transform parent);

參數

[NotNull] Transform parent:父對象的變換屬性,空值時會提示錯誤

返回值

若該對象是parent的子對象或更深層次的對象,則返回true;否則返回false

transform.IsChildOf(parent.transform); 

判斷一個物體是否是parent的子物體或更深層的物體


public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);

參數

Transform target:物體所看向的變換屬性

[DefaultValue("Vector3.up")] Vector3 worldUp:所指定的向上的向量,默認值為Vector3.up,即(0,1,0)

transform.LookAt(target.transform); transform.LookAt(target.transform,Vector3.down); 

旋轉變換組件,使得指向前方的向量指向目標位置

確定向前的向量(藍軸)后,需要確定繞藍軸旋轉的角度,這里使用一個表示物體上方向的向量來約束,注意這個參數是非強制性而是以引導的作用旋轉變換組件(優先確保先指向目標位置)

 

public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp);

參數

Vector3 worldPosition:物體所看向的位置

[DefaultValue("Vector3.up")] Vector3 worldUp:所指定的向上的向量,默認值為Vector3.up,即(0,1,0)

transform.LookAt(target.transform.position); transform.LookAt(target.transform.position,Vector3.down); 

此重載函數與上方類似,不再贅述


public void Rotate(Vector3 eulers, [DefaultValue("Space.Self")] Space relativeTo);

參數

Vector3 eulers:旋轉所繞的表示軸的向量(在世界坐標系下;考慮模長)

[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界坐標下還是自身坐標下,默認值是在自身坐標系下

transform.Rotate(Vector3.up,Space.World);//繞世界坐標系下的(0,1,0)方向,每幀旋轉1度 

繞eulers對應的軸進行旋轉,relativeTo確定在世界還是自身坐標系下

 

public void Rotate(float xAngle, float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo);

參數

float xAngle, float yAngle, float zAngle:旋轉所繞向量的三個軸上的信息(在世界坐標系下;考慮模長)

[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界坐標下還是自身坐標下,默認值是在自身坐標系下

transform.Rotate(0,5,0,Space.World);//繞世界坐標系下的(0,1,0)方向,每幀旋轉5度 

繞(x,y,z)對應的軸進行旋轉,relativeTo確定在世界還是自身坐標系下

 

public void Rotate(Vector3 axis, float angle, [DefaultValue("Space.Self")] Space relativeTo);

參數

Vector3 axis:所旋轉的軸(不考慮模長)

float angle:每幀旋轉的角度

[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界坐標下還是自身坐標下,默認值是在自身坐標系下

transform.Rotate(Vector3.up * 5, 1, Space.World);//繞世界坐標系下的(0,1,0)方向,每幀旋轉1度 

以給定角度定義的度數旋轉對象


public void RotateAround(Vector3 point, Vector3 axis, float angle);

參數

Vector3 point:旋轉的中心點

Vector3 axis:旋轉軸

float angle:每幀旋轉角度

transform.RotateAround(Vector3.zero, Vector3.up, 1);//繞世界坐標原點,(0,1,0)的方向,每幀1度的速度進行旋轉 

public void SetAsFirstSibling();

transform.SetAsFirstSibling(); 

將物體移動至所在層級的首位


public void SetAsLastSibling();

transform.SetAsLastSibling(); 

將物體移動至所在層級的末位


public void SetParent(Transform p);

public void SetParent(Transform parent, bool worldPositionStays);

參數

Transform p/Transform parent:父對象的變換屬性

bool worldPositionStays:父對象變換屬性是否保持原來不變

transform.SetParent(parent.transform); transform.SetParent(parent.transform, false); 

設置物體的父對象

當worldPositionStays設為false時,設置父子關系之后,子對象的transform組件保持與未建立關系之前一致;否則物體的在世界坐標下的位置保持不變


public void SetPositionAndRotation(Vector3 position, Quaternion rotation);

transform.SetPositionAndRotation(Vector3.zero, Quaternion.Euler(Vector3.up));//將物體設置為坐標原點,旋轉角度設為(0,1,0) 

設置物體在世界坐標下的位置與旋轉


public void SetSiblingIndex(int index);

參數

int index:要設置的索引

transform.SetSiblingIndex(1); 

設置物體所在在層級中的索引,同時會真正改變層級窗口中的位置


public Vector3 TransformDirection(Vector3 direction);

參數

Vector3 direction:表示某個方向的三維向量

返回值

世界坐標系下direction的方向向量

transform.TransformDirection(Vector3.forward);//將自身坐標系下的z軸方向轉換為世界坐標系下的方向 

將direction由自身坐標系轉換為世界坐標系下的向量表示

 

public Vector3 TransformDirection(float x, float y, float z);

參數

float x, float y, float z:表示方向的三個軸上的參數

返回值

世界坐標系下(x,y,z)的方向向量

transform.TransformDirection(0, 0, 1); 

將(x,y,z)由自身坐標系轉換為世界坐標系下的向量表示


public Vector3 TransformPoint(Vector3 position);

參數

Vector3 position:表示某個點(位置)的三維向量

返回值

世界坐標系下position的位置向量

transform.TransformPoint(Camera.main.transform.position);//將物體坐標系下的主相機的位置轉換為坐標系下的位置 

將position由自身坐標系轉換為世界坐標系下的向量表示

 

public Vector3 InverseTransformPoint(float x, float y, float z);

參數

float x, float y, float z:表示某個點(位置)的三個軸上的參數

返回值

世界坐標系下(x,y,z)的位置向量

transform.TransformPoint((0,0,0)); 

將(x,y,z)由自身坐標系轉換為世界坐標系下的向量表示

注意這個函數會受到縮放的影響


public Vector3 TransformVector(Vector3 vector);

參數

Vector3 vector:某個向量

返回值

世界坐標系下某個向量的表示

transform.TransformVector(Vector3.forward);//將物體自身坐標系下的z軸方向轉換為世界坐標系下的方向 

vector由物體坐標系轉換為世界的坐標系下的向量

 

public Vector3 TransformVector(float x, float y, float z);

參數

float x, float y, float z:表示某個向量的三個軸上的參數

返回值

世界坐標系下(x,y,z)的轉換后的向量

transform.TransformVector((0,0,0)); 

將(x,y,z)由物體自身坐標系轉換為世界坐標系下的向量

注意這個函數會受到縮放的影響


public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo);

參數

Vector3 translation:移動方向(考慮模長)

[DefaultValue("Space.Self")] Space relativeTo:相對自身或世界坐標系移動,默認為自身坐標系

transform.Translate(Vector3.forward,Space.World);//沿着世界坐標系下的前進方向,按每幀1單位的速度平移物體 

沿translation的方向,以Space relativeTo的坐標系,根據translation的速度進行平移

 

public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);

參數

float x, float y, float z:移動方向在三個軸上的屬性

[DefaultValue("Space.Self")] Space relativeTo:相對自身或世界坐標系移動,默認為自身坐標系

transform.Translate(0, 0, 1,Space.World); 

沿(x,y,z)的方向,以Space relativeTo的坐標系,(x,y,z)速度進行平移

 

public void Translate(Vector3 translation, Transform relativeTo);

public void Translate(float x, float y, float z, Transform relativeTo);

參數

Vector3 translation:移動方向(考慮模長)

float x, float y, float z:移動方向在三個軸上的屬性

relativeTo:相對移動的變換屬性

transform.Translate(Vector3.up, Camera.main.transform);//相對相機,向上方進行移動 transform.Translate(0,1,0,Camera.main.transform);//相對相機,向上方進行移動 

相對relativeTo的transform屬性進行移動

relativeTo為空時,則相對世界坐標移動


免責聲明!

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



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