Transform類
1. 官方定義
class in UnityEngine/Inherits from:Component/Implemented in:UnityEngine.CoreModule
屬於UnityEngine命名空間下的類,繼承自Component類,是UnityEngine.CoreModule的核心模塊類
2. 官方描述
Position, rotation and scale of an object.
Every object in a Scene has a Transform. It’s used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators so you can loop through children using:
using UnityEngine;
public class Example : MonoBehaviour
{
// Moves all transform children 10 units upwards!
void Start()
{
foreach (Transform child in transform)
{
child.position += Vector3.up * 10.0f;
}
}
}
See Also: The component reference, Physics class.
該類用於表示物體的位置旋轉和縮放,用法比較簡單,但是原理涉及到3D數學,像全局和局部座標系,向量公式,歐拉角和四元數等,好在Unity幫我們解決了有關數學的複雜運算,我們的只需指定目標參數就可以實現我們想要的結果。這裏提一下座標系、歐拉角和四元數:
- 全局座標系和局部座標系
Unity使用的是左手座標系,因爲場景中物體可能存在父子關係,於是引入全局座標系和局部座標系的概念,對於單個物體,Unity場景中會默認一個座標系,這個座標系在場景中有一個原點,而單個物體的座標便是面向該場景中這個默認原點的座標,局部座標系是以單個物體爲參考對象,比如場景中中有一個物體,這個物體以自己爲參考便會有一個對於自己的局部座標系;場景中有多個物體時,爲了方便理解,假設有兩個物體,這兩個物體具備父子關係,我們移動子物體時,其實子物體的座標是相對於父物體而言的。注意在Unity的工具欄中有一個Local和Global的按鈕,在一般情況下,選中物體後點擊按鈕查看,物體的座標系不會有明顯區別,可以給予物體一個特殊的旋轉方位。再次點擊按鈕會發現全局座標系的顯示與場景視圖右上角的座標顯示相同,而局部座標系是根據物體自身位置發生改變。這裏再提一下Local/Global按鈕左邊的Pivot/Center按鈕,Pivot表示物體的默認原點,而Center是Unity根據網格計算中心點,可以在場景中創建兩個物體,然後在Hierarchy視圖選中這兩個物體,注意物體不要重疊,最好分開方便觀察,點擊按鈕會發現中心點的位置變化。 - 歐拉角
物體在三位空間中的旋轉可以根據座標軸分步進行,從而實現空間旋轉變化,而這三個角分別被稱爲章動角,旋進角,自轉角,他們統稱爲歐拉角,歐拉角的選取方法非常多,但是都可以達到實現目標,Unity中根據ZXY軸的順序進行旋轉,我們在介紹eulerAngles屬性時再來討論具體用法。 - 四元數
四元數時一種超複數,數學定義爲A=a + bi+ cj + dk,其中ijk爲虛數單位,四元數的幾何意義中,可以用ijk表示旋轉,所以Unity通過Quaternion類定義rotation屬性,注意eulerAngles屬性由 Vector3類定義,四元數和歐拉角因爲都是起旋轉作用,兩個之前可以切換。
因Transform類涉及到複雜的數學概念,而且裏面包含的屬性和方法過多,下面先列舉該類包含內容,後面我分節講解。
3. 屬性
childCount:The number of children the parent Transform has.
eulerAngles:The rotation as Euler angles in degrees.
forward:Returns a normalized vector representing the blue axis of the transform in world space.
hasChanged:Has the transform changed since the last time the flag was set to ‘false’?
hierarchyCapacity:The transform capacity of the transform’s hierarchy data structure.
hierarchyCount:The number of transforms in the transform’s hierarchy data structure.
localEulerAngles:The rotation as Euler angles in degrees relative to the parent transform’s rotation.
localPosition:Position of the transform relative to the parent transform.
localRotation:The rotation of the transform relative to the transform rotation of the parent.
localScale:The scale of the transform relative to the GameObjects parent.
localToWorldMatrix:Matrix that transforms a point from local space into world space (Read Only).
lossyScale:The global scale of the object (Read Only).
parent:The parent of the transform.
position:The world space position of the Transform.
right:The red axis of the transform in world space.
root:Returns the topmost transform in the hierarchy.
rotation:A Quaternion that stores the rotation of the Transform in world space.
up:The green axis of the transform in world space.
worldToLocalMatrix:Matrix that transforms a point from world space into local space (Read Only).
4. 公有方法
DetachChildren:Unparents all children.
Find:Finds a child by n and returns it.
GetChild:Returns a transform child by index.
GetSiblingIndex:Gets the sibling index.
InverseTransformDirection:Transforms a direction from world space to local space. The opposite of Transform.TransformDirection.
InverseTransformPoint:Transforms position from world space to local space.
InverseTransformVector:Transforms a vector from world space to local space. The opposite of Transform.TransformVector.
IsChildOf:Is this transform a child of parent?
LookAt:Rotates the transform so the forward vector points at /target/'s current position.
Rotate:Use Transform.Rotate to rotate GameObjects in a variety of ways. The rotation is often provided as an Euler angle and not a Quaternion.
RotateAround:Rotates the transform about axis passing through point in world coordinates by angle degrees.
SetAsFirstSibling:Move the transform to the start of the local transform list.
SetAsLastSibling:Move the transform to the end of the local transform list.
SetParent:Set the parent of the transform.
SetPositionAndRotation:Sets the world space position and rotation of the Transform component.
SetSiblingIndex:Sets the sibling index.
TransformDirection:Transforms direction from local space to world space.
TransformPoint:Transforms position from local space to world space.
TransformVector:Transforms vector from local space to world space.
Translate:Moves the transform in the direction and distance of translation.