U3D Transform用法


  最近在學習unity3d,下面對Transform類做一個小結

一、常用屬性和方法

  1.1 常用屬性:

用代碼展示一下上面的一些屬性,值得注意的是myCube是mySphere的父物體

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); } }

結果如下:

  

  position 和localPosition的區別:

  position是世界坐標中transform的位置,而localPosition是相對於父級變換的位置,即以父級物體的坐標為原點,它的相對位置。

  

  eulerAngles:歐拉角,當rotate小於中的各個值都小於360時,使用eulerAngles將可以正確的將rotate的值轉換為歐拉角

   x、y、z角代表繞z軸旋轉z度,繞x軸旋轉x度,繞y軸旋轉y度(這個順序)。

  僅使用這個變量讀取和設置角度為絕對值。不要遞增它們,當超過角度360度,它將錯誤。使用Transform.Rotate替代。

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軸旋轉

 

二、Transform完整的內容(來自unity聖典)

  Variables 變量

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).
變換點的矩陣從世界坐標到局部坐標(只讀)。

  Functions 函數

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的方向和距離。 

 

 

 


免責聲明!

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



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