最近在學習unity3d,下面對Transform類作一個小結函數
1.1 經常使用屬性:學習
用代碼展現一下上面的一些屬性,值得注意的是myCube是mySphere的父物體this
using UnityEngine; public class test : MonoBehaviour { GameObject myCube; GameObject mySphere; void Start() { myCube = GameObject.Find("Cube"); mySphere = GameObject.Find("Sphere"); Debug.Log("Cube position:" + myCube.transform.position); Debug.Log("Sphere position:" + mySphere.transform.position); Debug.Log("Sphere localPosition:" + mySphere.transform.localPosition); Debug.Log("eulerAngle" + myCube.transform.eulerAngles); } }
結果以下:spa
position 和localPosition的區別:3d
position是世界座標中transform的位置,而localPosition是相對於父級變換的位置,即以父級物體的座標爲原點,它的相對位置。code
eulerAngles:歐拉角,當rotate小於中的各個值都小於360時,使用eulerAngles將能夠正確的將rotate的值轉換爲歐拉角orm
x、y、z角表明繞z軸旋轉z度,繞x軸旋轉x度,繞y軸旋轉y度(這個順序)。對象
僅使用這個變量讀取和設置角度爲絕對值。不要遞增它們,當超過角度360度,它將錯誤。使用Transform.Rotate替代。blog
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float yRotation = 5.0F; void Update() { yRotation += Input.GetAxis("Horizontal"); transform.eulerAngles = new Vector3(10, yRotation, 0); } void Example() { print(transform.eulerAngles.x); print(transform.eulerAngles.y); print(transform.eulerAngles.z); } }
不要分別設置歐拉角其中一個軸(例如: eulerAngles.x = 10; ),由於這將致使偏移和不但願的旋轉。當設置它們一個新的值時,要同時設置所有,如上所示。索引
translate:可使物體向某方向移動物體多少距離。它有許多個重載
void Translate(Vector3 translation, Space relativeTo = Space.Self): 移動transform在translation的方向和距離。
若是relativeTo留空或者設置爲Space.Self,移動被應用相對於變換的自身軸。(當在場景視圖選擇物體時,x、y和z軸顯示)若是相對於Space.World 移動被應用相對於世界座標系統。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Translate(Vector3.forward * Time.deltaTime); transform.Translate(Vector3.up * Time.deltaTime, Space.World); } }
void Translate(float x, float y, float z, Space relativeTo = Space.Self):: 移動變換由x沿着x軸,y沿着y軸,z沿着z軸。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Translate(0, 0, Time.deltaTime); transform.Translate(0, Time.deltaTime, 0, Space.World); } }
還有一些這裏就不一一例舉了
rotate:旋轉
transform.Rotate(0, 90, 0);//分別繞X,Y,Z軸旋轉
transform.Rotate(Vector3.right * Time.deltaTime);//以歐拉角旋轉,順序是ZXY,right是向X軸旋轉1度
transform.Rotate(0, 45, 0, Space.World);//繞世界座標系的XYZ軸旋轉,也就是其頂層父物體的座標系,若是自身在頂層則沒有區別,並非繞點旋轉,而是不按照自身的座標系旋轉
transform.Rotate(Vector3.up, Space.Self);//繞自身座標系旋轉,這是默認的
void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self):按照angle度圍繞axis軸旋轉變換。
下面例子將使掛載腳本的物體繞Y軸不停地每次旋轉45度
using UnityEngine; public class MyRotate : MonoBehaviour { void Update() { transform.Rotate(Vector3.up*Time.deltaTime, 45); } }
在代碼中直接使用小寫的transform表明的是掛載該腳本的物體的transform
控制物體旋轉的時候可能會弄不明白讓它沿着哪一個軸轉,我的總結出了一個小技巧,即你在Rotate中使用了哪一個軸就表明繞哪一個軸轉,上面的代碼用的是Vector3.up,使用了包含Y軸的參數,因此是按Y軸旋轉,當改爲
Vector3.back則繞X軸旋轉
childCount | The number of children the Transform has. 該變換的子對象數量。 |
eulerAngles | The rotation as Euler angles in degrees. 此旋轉做爲歐拉角度。 |
forward | The blue axis of the transform in world space. 在世界空間座標,變換的藍色軸。也就是z軸。 |
hasChanged | Has the transform changed since the last time the flag was set to 'false'? 此變換自從上次標識是否被設置爲false了? |
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 parent transform's rotation. 該變換的旋轉角度相對於父級變換的旋轉角度。 |
localScale | The scale of the transform relative to the 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 position of the transform in world space. 在世界空間座標transform的位置。 |
right | The red axis of the transform in world space. 在世界座標空間,變換的紅色軸。也就是x軸。 |
root | Returns the topmost transform in the hierarchy. 返回最高層級的變換。 |
rotation | The rotation of the transform in world space stored as a Quaternion. 在世界座標空間,儲存爲四元數變換的旋轉角度。 |
up | The green axis of the transform in world space. 在世界座標空間,變換的綠色軸。也就是Y軸。 |
worldToLocalMatrix | Matrix that transforms a point from world space into local space (Read Only). 變換點的矩陣從世界座標到局部座標(只讀)。 |
DetachChildren | Unparents all children.、 全部子對象解除父子關係。 |
Find | Finds a child by name 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. 變換的方向從世界座標轉換到局部座標。和Transform.TransformDirection相反。 |
InverseTransformPoint | Transforms position from world space to local space. The opposite of Transform.TransformPoint. 變換位置從世界座標到局部座標。和Transform.TransformPoint相反。 |
InverseTransformVector | Transforms a vector from world space to local space. The opposite of Transform.TransformVector. 變換一個向量從世界座標空間到局部座標空間。這個操做與Transform.TransformVector相反。 |
IsChildOf | Is this transform a child of /parent/? 這個變換是parent的子對象? |
LookAt | Rotates the transform so the forward vector points at /target/'s current position. 旋轉此變換,讓向前向量指向target的當前位置。(照相機的視口對準目標) |
Rotate | Applies a rotation of /eulerAngles.z/ degrees around the z axis, /eulerAngles.x/ degrees around the x axis, and /eulerAngles.y/ degrees around the y axis (in that order). 應用一個歐拉角的旋轉角度,eulerAngles.z度圍繞z軸,eulerAngles.x度圍繞x軸,eulerAngles.y度圍繞y軸(這樣的順序)。 |
RotateAround | Rotates the transform about axis passing through point in world coordinates by angle degrees. 圍繞世界座標的point點的axis旋轉該變換angle度。 |
SetAsFirstSibling | Move the transform to the start of the local transfrom list. 移動該變換到此局部變換列表的開始。 |
SetAsLastSibling | Move the transform to the end of the local transfrom list. 移動該變換到此局部變換列表的末尾。 |
SetParent | Set the parent of the transform. 設置該變換的父級。 |
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. 移動transform在translation的方向和距離。 |