Transform
2015-11-01 22:51
197 查看
http://docs.unity3d.com/ScriptReference/Transform.html
继承自:Component
一个对象的位置、旋转和缩放。
场景中每个对象都有一个Transform。它被用来存储和操作对象的位置、旋转和缩放。每一个Transform都可以有一个父级,允许你分层次的运用位置、旋转和缩放。Hierarchy面板有层级关系。他们也支持计数器,所以你可以遍历子物体。
childCount
类型:int
Transform的子物体的个数。(只返回一级子物体的个数,子物体下的子物体不计数)
eulerAngles
类型:Vector3
以角度为单位,以欧拉角计算的旋转。
x, y, z角代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度。
仅使用这个变量来读取和设置脚的绝对的值。不要递增他们,超过360度时它将失败。使用Transform.Rotate来代替。
不要单独的设置一个轴的角度,因为它将导致偏移和不希望的旋转。当设置一个新值时,一次性全部设置他们,就像上面所示。unity会转换欧拉角度到Transform.rotation或将Transform.rotation转换到欧拉角度。
forward
类型:Vector3
世界坐标系中蓝色的轴,也就是Z轴。
hasChanged
类型:bool
上一次这个标签设置为fsalse后transform改变过吗?
对变换的任何改变会导致矩阵的重新计算:任意调节它的位置、旋转或缩放。请注意此操作,是否在设置此标识之前新旧的值不同,也将不会实际检查。因此,对于实例化,transform.position将总是设置此变换的hasChanged,无论是否有实际的变化。
localEulerAngle
类型:Vector3
以角度为单位,相对于父级transform的旋转。
x, y, z角度代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度。
仅使用这个变量去读取和设置绝对的角度的值。不要递增它们,当角度超过360度时它将失败,使用Transform.Rotate代替。
Unity自动转换这些角度到Transform.loaclRotation,反之亦然。
localPosition
类型:Vector3
相对于父级transform,当前transform的位置。
如果当前transform没有父级,它与Transform.position的值一样。
注意当计算世界坐标系的位置时,父级transform的世界坐标系的旋转和缩放会作用于当前位置。也就是说,Transform.position的1unit就是1unit,而transform.localposition的1unit会因为父级的缩放而缩放。
localScale
类型:Vector3
相对于父级transform,当前transform的缩放。
localRotation
类型:Quaternion
相对于父级transform,当前transform的旋转。
Unity以四元数来存储rotation。要旋转一个对象,使用Transform.Rotate。要以欧拉角度来改变旋转,使用Transform.localEulerAngles。
localToWorldMatrix
类型:matrix4x4
一个点从自身坐标系转换到世界坐标系的矩阵。(只读)
如果你对使用矩阵来转换坐标不熟悉,使用 Transform.TransformPoint代替。
重要:如果你在设置着色器参数你必须使用Renderer.localToWoldMarix代替。
lossyScale
类型:Vector3
对象的全局缩放(只读)(世界坐标系的缩放)
请注意如果你有一个缩放的父级transform并且子级是任意旋转,那么这个缩放有偏差。因此缩放不能由3个组件的向量正确表示,而是用3X3的矩阵。然而这种表述方式工作起来非常不方便。lossyScale是一个非常方便的属性来表示真实世界的缩放。如果你的对象没有偏差,这个值将是完全正确的,如果物体包含偏差,这个值也不会有很大不同。
(这个值就是物体在世界坐标系中的缩放,缩放是有损的是因为并不精确,它就是错误的如果它的一个或者多个父级有不同的x, y, z的缩放或者旋转。如果父级的缩放是统一的那么它就是完全正确的。如果父级的旋转是:0.1,0.5,0.4,lossyScale会接近但并不是非常精确。如果父级的缩放是:0.1,0.1,0.1,lossyScale是精确的。)
parent
类型:Transform
transform的父级
改变父级会改变相对于父级的位置、缩放和旋转信息,但是不会改变世界坐标系的位置、旋转和缩放。
另一个例子:
position
类型:Vector3
transform在世界坐标系的位置
right
类型:Vector3
对象的自身坐标系的x轴在世界坐标系中指向的方向。
root
类型:Transform
层级关系中最上层的transform。
(永远不会返回null,如果transform没有父级则返回他自己)
rotation
类型:Quaternion
保存在四元数中的transform在世界坐标系中的旋转。
Unity以四元数保存旋转。要旋转一个对象,使用Transform.Rotate。要以欧拉角设置旋转,使用Transform.eulerAngles
另一个例子:
up
类型:Vector3
世界坐标系中,transform的绿色的轴。
worldToLocalMatrix
类型:Matrix4x4
矩阵转换一个点从世界坐标系到自身坐标系(只读)
如果你不熟悉使用矩阵来转换坐标,使用Transform.InverseTransformPoint 代替
重要:如果设置着色器参数,你必须使用Renderer.worldToLocalMatrix代替
DetachChildren
类型:void DetachChildren()
和所有子级解除父子关系。
如果你想销毁一个层级关系的根对象而不想销毁子对象,会用到这个函数。
Find
类型:Transform Find(string name)
参数:
name: 要找的子级的名字。
通过name找到一个子级并返回它。
如果没有找到name子级,返回null。如果name包含一个'/'字符,它会将其作为层级的路径名。(只返回一级子级,多级子级必须按路径查找)
GetChild
类型:Transform GetChild(int index)
参数:
index: 要返回的子transform的索引。必须比Transform.childCount小。
返回: 索引为index的子级transform
GetSiblingIndex
类型:int GetSiblingIndex()
会的同级索引。(即自己在所在层级的索引)
InverseTransformDirection
类型:Vector3 InverseTransformDirection(Vector3 direction)
从世界坐标系转换一个direction到自身坐标系。与Transform.TransformDirection相反
这个操作不受缩放影响。
你用该使用Transform.InverseTransformPoint如果向量是空间的点而不是方向。
2. public Vector3 InverseTransformDirection(float x, float y, float z)
同上
InverseTransformPoint
类型:Vector3 InverseTransformPoint(Vector3 position)
将position从世界坐标系转换到自身坐标系。
这个函数与Transform.TransformPoint相反,后者被用来从自身坐标系转换到世界坐标系。
注意返回值受缩放影响。如果你在处理方向向量而不是位置向量,使用Transform.InverseTransformDirection。
2. public Vector3 InverseTransformPoint(float x,
float y,
float z);
同上
InverseTransformVector
类型:Vector3 InverseTransformVector(Vector3 vector)
将一个vector从世界坐标系转换到自身坐标系。Transform.TransformVector函数的反面。受缩放影响。
2. public Vector3 InverseTransformVector(float x,
float y,
float z);
同上
IsChildOf
类型:bool IsChildOf(Transform parent)
当前的transform是否是parent的子级?
返回一个布尔值表明这个transform是否是给出的transform的子级。如果这个transform是个子级、多级子级(子级的子级)或完全就是它自己,返回true,否则返回false。
LookAt
类型: void LookAt(Transform target, Vector3 worldUp = Vector3.up);
参数:
target : 要朝向的对象
worldUp: 指定向上的方向的向量
旋转transform,使前方朝向target的位置。
然后它旋转transform使他的向上的方向向量指向worldup向量示意的方向。如果worldup参数为空,函数默认使用世界坐标系y轴。worldup只是一个示意向量。如果向前的方向垂直于worldup,向上的向量只匹配worldup向量。
// Drag another object onto it to make the camera look at it.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}[/code]
2. public void LookAt(Vector3 worldPosition, Vector3 worldUp =
Vector3.up);
同上
Rotate
类型:Rotate(Vector3 eulerAngles, Soace relativeTo=Space.Self)
绕z轴旋转eulerAngles.z度,绕x轴旋转eulerAngles.x度,绕y轴旋转eulerAngles.y度(按照这个顺序)。
如果没有设置relativeTo,旋转绕着自身坐标系旋转。如果relativeTo设置为Space.World,绕世界坐标系旋转
2. public void Rotate(float xAngle,
float yAngle,
float zAngle, SpacerelativeTo =
Space.Self);
同上
3. public void Rotate(Vector3 axis,
float angle, Space relativeTo =
Space.Self);
绕着axis旋转angle度。
RotateAround
类型: void RotateAround(Vector3 point, Vector3 axis, float angle);
世界坐标系中,绕着穿过point点的axis轴旋转angle度。
这个函数同时改变transform的位置和旋转
SetAsFirstSibling
类型:void SetAsFirstSibling()
移动transform到自身所在transform列表的开端。(把transform移动到当前层级的第一个位置)
SetAsLastSibling
类型: void SetAsLastSibling();
移动transform到transform列表的末端。
SetParent
类型: void SetParent(Transform parent, bool worldPositionStays)
参数:
parent: transform要使用的父级
worldPositionStays: 如果为真,相对父级的位置、缩放和旋转被改变所以对象可以保持以前的世界坐标系中的位置、旋转和缩放。
设置transform的父级。
这个函数与parent属性一样,除了它可以通过设置worldPositionStays属性为false让transform保持他的局部坐标不变。
SetSiblingIndex
类型:void SetSiblingIndex(int index)
参数:
index: 设置的索引
设置当前层级索引。
TransformDirection
类型:Vector3 TransformDirection(Vection direction)
把direction从自身坐标系转换到世界坐标系。
这个操作不受transform的缩放和位置影响。
返回的向量与direction长度相同。
如果向量代表的是一个位置而不是方向,使用Transform.TransformPoint。
2. public Vector3 TransformDirection(float x,
float y,
float z);
同上
TransformPoint
类型:Vector3 TransformPoint(Vector3 position)
把position从自身坐标系转换到世界坐标系
注意返回的位置受缩放影响。
2. public Vector3 TransformPoint(float x,
float y,
float z);
同上
TransformVector
类型: Vector3 TransformVector(Vector3 vector)
操作不受transform的位置影响,但是受缩放影响。返回的向量可能与vector长度不一样。
2. public Vector3 TransformVector(float x,
float y,
float z);
同上
Translate
类型:void Translate(Vector3 translation, Space relativeTo=Space.Self)
将transform以translation的方向和距离移动。
如果relativeTo没有设置或设置为Space.Self,移动是相对于自身坐标系。如果relativeTo设置为Space.World,移动是相对于世界坐标系。
2.public void Translate(float x,
float y,
float z, Space relativeTo =
Space.Self);
同上
3.public void Translate(Vector3 translation, Transform relativeTo);
4.public void Translate(float x,
float y,
float z, Transform relativeTo);
Transform
继承自:Component一个对象的位置、旋转和缩放。
场景中每个对象都有一个Transform。它被用来存储和操作对象的位置、旋转和缩放。每一个Transform都可以有一个父级,允许你分层次的运用位置、旋转和缩放。Hierarchy面板有层级关系。他们也支持计数器,所以你可以遍历子物体。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { foreach (Transform child in transform) { child.position += Vector3.up * 10.0F; } } }
Variables
childCount类型:int
Transform的子物体的个数。(只返回一级子物体的个数,子物体下的子物体不计数)
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { print(transform.childCount); } }
eulerAngles
类型:Vector3
以角度为单位,以欧拉角计算的旋转。
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); } }
不要单独的设置一个轴的角度,因为它将导致偏移和不希望的旋转。当设置一个新值时,一次性全部设置他们,就像上面所示。unity会转换欧拉角度到Transform.rotation或将Transform.rotation转换到欧拉角度。
forward
类型:Vector3
世界坐标系中蓝色的轴,也就是Z轴。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float thrust; public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { rb.AddForce(transform.forward * thrust); } }
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float angleBetween = 0.0F; public Transform target; void Update() { Vector3 targetDir = target.position - transform.position; angleBetween = Vector3.Angle(transform.forward, targetDir); } }
hasChanged
类型:bool
上一次这个标签设置为fsalse后transform改变过吗?
对变换的任何改变会导致矩阵的重新计算:任意调节它的位置、旋转或缩放。请注意此操作,是否在设置此标识之前新旧的值不同,也将不会实际检查。因此,对于实例化,transform.position将总是设置此变换的hasChanged,无论是否有实际的变化。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { if (transform.hasChanged) { print("The transform has changed!"); transform.hasChanged = false; } } }
localEulerAngle
类型:Vector3
以角度为单位,相对于父级transform的旋转。
x, y, z角度代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度。
仅使用这个变量去读取和设置绝对的角度的值。不要递增它们,当角度超过360度时它将失败,使用Transform.Rotate代替。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { print(transform.localEulerAngles.x); print(transform.localEulerAngles.y); print(transform.localEulerAngles.z); } }
Unity自动转换这些角度到Transform.loaclRotation,反之亦然。
localPosition
类型:Vector3
相对于父级transform,当前transform的位置。
如果当前transform没有父级,它与Transform.position的值一样。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { transform.localPosition = new Vector3(0, 0, 0); print(transform.localPosition.y); } }
注意当计算世界坐标系的位置时,父级transform的世界坐标系的旋转和缩放会作用于当前位置。也就是说,Transform.position的1unit就是1unit,而transform.localposition的1unit会因为父级的缩放而缩放。
localScale
类型:Vector3
相对于父级transform,当前transform的缩放。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { // Widen the object by 0.1 transform.localScale += new Vector3(0.1F, 0, 0); } }
localRotation
类型:Quaternion
相对于父级transform,当前transform的旋转。
Unity以四元数来存储rotation。要旋转一个对象,使用Transform.Rotate。要以欧拉角度来改变旋转,使用Transform.localEulerAngles。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { transform.localRotation = Quaternion.identity; } }
localToWorldMatrix
类型:matrix4x4
一个点从自身坐标系转换到世界坐标系的矩阵。(只读)
如果你对使用矩阵来转换坐标不熟悉,使用 Transform.TransformPoint代替。
重要:如果你在设置着色器参数你必须使用Renderer.localToWoldMarix代替。
lossyScale
类型:Vector3
对象的全局缩放(只读)(世界坐标系的缩放)
请注意如果你有一个缩放的父级transform并且子级是任意旋转,那么这个缩放有偏差。因此缩放不能由3个组件的向量正确表示,而是用3X3的矩阵。然而这种表述方式工作起来非常不方便。lossyScale是一个非常方便的属性来表示真实世界的缩放。如果你的对象没有偏差,这个值将是完全正确的,如果物体包含偏差,这个值也不会有很大不同。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { print(transform.lossyScale); } }
(这个值就是物体在世界坐标系中的缩放,缩放是有损的是因为并不精确,它就是错误的如果它的一个或者多个父级有不同的x, y, z的缩放或者旋转。如果父级的缩放是统一的那么它就是完全正确的。如果父级的旋转是:0.1,0.5,0.4,lossyScale会接近但并不是非常精确。如果父级的缩放是:0.1,0.1,0.1,lossyScale是精确的。)
parent
类型:Transform
transform的父级
改变父级会改变相对于父级的位置、缩放和旋转信息,但是不会改变世界坐标系的位置、旋转和缩放。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject player; //Invoked when a button is pressed. public void Example(GameObject newParent) { //Makes the GameObject "newParent" the parent of the GameObject "player". player.transform.parent = newParent.transform; //Display the parent's name in the console. Debug.Log ("Player's Parent: " + player.transform.parent.name); // Check if the new parent has a parent GameObject. if(newParent.transform.parent != null) { //Display the name of the grand parent of the player. Debug.Log ("Player's Grand parent: " + player.transform.parent.parent.name); } } }
另一个例子:
// Detaches the transform from its parent. transform.parent = null;
position
类型:Vector3
transform在世界坐标系的位置
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { transform.position = new Vector3(0, 0, 0); print(transform.position.x); } }
right
类型:Vector3
对象的自身坐标系的x轴在世界坐标系中指向的方向。
root
类型:Transform
层级关系中最上层的transform。
(永远不会返回null,如果transform没有父级则返回他自己)
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void OnCollisionEnter(Collision collision) { if (collision.transform.root != transform.root) print("The colliding objects are not in the same hierarchy"); } }
rotation
类型:Quaternion
保存在四元数中的transform在世界坐标系中的旋转。
Unity以四元数保存旋转。要旋转一个对象,使用Transform.Rotate。要以欧拉角设置旋转,使用Transform.eulerAngles
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { transform.rotation = Quaternion.identity; } }
另一个例子:
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float smooth = 2.0F; public float tiltAngle = 30.0F; void Update() { float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle; float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle; Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ); transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); } }
up
类型:Vector3
世界坐标系中,transform的绿色的轴。
worldToLocalMatrix
类型:Matrix4x4
矩阵转换一个点从世界坐标系到自身坐标系(只读)
如果你不熟悉使用矩阵来转换坐标,使用Transform.InverseTransformPoint 代替
重要:如果设置着色器参数,你必须使用Renderer.worldToLocalMatrix代替
Public Functions
DetachChildren类型:void DetachChildren()
和所有子级解除父子关系。
如果你想销毁一个层级关系的根对象而不想销毁子对象,会用到这个函数。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { transform.DetachChildren(); Destroy(gameObject); } }
Find
类型:Transform Find(string name)
参数:
name: 要找的子级的名字。
通过name找到一个子级并返回它。
如果没有找到name子级,返回null。如果name包含一个'/'字符,它会将其作为层级的路径名。(只返回一级子级,多级子级必须按路径查找)
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject player; public GameObject gun; public Transform ammo; //Invoked when a button is clicked. public void Example() { //Finds and assigns the child of the player named "Gun". gun = player.transform.Find("Gun").gameObject; //If the child was found. if(gun != null) { //Find the child named "ammo" of the gameobject "magazine" (magazine is a child of "gun"). ammo = gun.transform.Find("magazine/ammo"); } else Debug.Log("No child with the name 'Gun' attached to the player"); } }
GetChild
类型:Transform GetChild(int index)
参数:
index: 要返回的子transform的索引。必须比Transform.childCount小。
返回: 索引为index的子级transform
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public Transform meeple; public GameObject grandChild; public void Example() { //Assigns the transform of the first child of the Game Object this script is attached to. meeple = this.gameObject.transform.GetChild(0); //Assigns the first child of the first child of the Game Object this script is attached to. grandChild = this.gameObject.transform.GetChild(0).GetChild(0).gameObject; } }
GetSiblingIndex
类型:int GetSiblingIndex()
会的同级索引。(即自己在所在层级的索引)
InverseTransformDirection
类型:Vector3 InverseTransformDirection(Vector3 direction)
从世界坐标系转换一个direction到自身坐标系。与Transform.TransformDirection相反
这个操作不受缩放影响。
你用该使用Transform.InverseTransformPoint如果向量是空间的点而不是方向。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { private Vector3 relative; void Example() { relative = transform.InverseTransformDirection(Vector3.forward); Debug.Log(relative); } }
2. public Vector3 InverseTransformDirection(float x, float y, float z)
同上
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { private Vector3 relative; void Example() { relative = transform.InverseTransformDirection(0, 0, 1); Debug.Log(relative); } }
InverseTransformPoint
类型:Vector3 InverseTransformPoint(Vector3 position)
将position从世界坐标系转换到自身坐标系。
这个函数与Transform.TransformPoint相反,后者被用来从自身坐标系转换到世界坐标系。
注意返回值受缩放影响。如果你在处理方向向量而不是位置向量,使用Transform.InverseTransformDirection。
// Calculate the transform's position relative to the camera. using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public Transform cam; public Vector3 cameraRelative; void Start() { cam = Camera.main.transform; Vector3 cameraRelative = cam.InverseTransformPoint(transform.position); if (cameraRelative.z > 0) print("The object is in front of the camera"); else print("The object is behind the camera"); } }
2. public Vector3 InverseTransformPoint(float x,
float y,
float z);
同上
// Calculate the world origin relative to this transform. using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Start() { Vector3 relativePoint = transform.InverseTransformPoint(0, 0, 0); if (relativePoint.z > 0) print("The world origin is in front of this object"); else print("The world origin is behind of this object"); } }
InverseTransformVector
类型:Vector3 InverseTransformVector(Vector3 vector)
将一个vector从世界坐标系转换到自身坐标系。Transform.TransformVector函数的反面。受缩放影响。
2. public Vector3 InverseTransformVector(float x,
float y,
float z);
同上
IsChildOf
类型:bool IsChildOf(Transform parent)
当前的transform是否是parent的子级?
返回一个布尔值表明这个transform是否是给出的transform的子级。如果这个transform是个子级、多级子级(子级的子级)或完全就是它自己,返回true,否则返回false。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void OnTriggerEnter(Collider col) { if (col.transform.IsChildOf(transform)) return; print("Do something here"); } }
LookAt
类型: void LookAt(Transform target, Vector3 worldUp = Vector3.up);
参数:
target : 要朝向的对象
worldUp: 指定向上的方向的向量
旋转transform,使前方朝向target的位置。
然后它旋转transform使他的向上的方向向量指向worldup向量示意的方向。如果worldup参数为空,函数默认使用世界坐标系y轴。worldup只是一个示意向量。如果向前的方向垂直于worldup,向上的向量只匹配worldup向量。
// This complete script can be attached to a camera to make it // continuously point at another object. // The [code]targetvariable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}[/code]
2. public void LookAt(Vector3 worldPosition, Vector3 worldUp =
Vector3.up);
同上
Rotate
类型:Rotate(Vector3 eulerAngles, Soace relativeTo=Space.Self)
绕z轴旋转eulerAngles.z度,绕x轴旋转eulerAngles.x度,绕y轴旋转eulerAngles.y度(按照这个顺序)。
如果没有设置relativeTo,旋转绕着自身坐标系旋转。如果relativeTo设置为Space.World,绕世界坐标系旋转
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Rotate(Vector3.right * Time.deltaTime); transform.Rotate(Vector3.up * Time.deltaTime, Space.World); } }
2. public void Rotate(float xAngle,
float yAngle,
float zAngle, SpacerelativeTo =
Space.Self);
同上
3. public void Rotate(Vector3 axis,
float angle, Space relativeTo =
Space.Self);
绕着axis旋转angle度。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Rotate(Vector3.right, Time.deltaTime); transform.Rotate(Vector3.up, Time.deltaTime, Space.World); } }
RotateAround
类型: void RotateAround(Vector3 point, Vector3 axis, float angle);
世界坐标系中,绕着穿过point点的axis轴旋转angle度。
这个函数同时改变transform的位置和旋转
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime); } }
SetAsFirstSibling
类型:void SetAsFirstSibling()
移动transform到自身所在transform列表的开端。(把transform移动到当前层级的第一个位置)
using UnityEngine; using System.Collections; using UnityEngine.UI; //Required when using UI Elements. using UnityEngine.EventSystems; // Required when using event data. public class ExampleClass : MonoBehaviour, IPointerDownHandler { public RectTransform panelRectTransform; //Invoked when the mouse pointer goes down on a UI element. public void OnPointerDown (PointerEventData data) { // Puts the panel to the back as it is now the first UI element to be drawn. panelRectTransform.SetAsFirstSibling (); } }
SetAsLastSibling
类型: void SetAsLastSibling();
移动transform到transform列表的末端。
using UnityEngine; using System.Collections; using UnityEngine.UI; //Required when using UI Elements. using UnityEngine.EventSystems; // Required when using event data. public class ExampleClass : MonoBehaviour, IPointerDownHandler { public RectTransform panelRectTransform; //Invoked when the mouse pointer goes down on a UI element. public void OnPointerDown (PointerEventData data) { // Puts the panel to the front as it is now the last UI element to be drawn. panelRectTransform.SetAsLastSibling (); } }
SetParent
类型: void SetParent(Transform parent, bool worldPositionStays)
参数:
parent: transform要使用的父级
worldPositionStays: 如果为真,相对父级的位置、缩放和旋转被改变所以对象可以保持以前的世界坐标系中的位置、旋转和缩放。
设置transform的父级。
这个函数与parent属性一样,除了它可以通过设置worldPositionStays属性为false让transform保持他的局部坐标不变。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject player; //Invoked when a button is clicked. public void Example(Transform newParent) { //Sets "newParent" as the new parent of the player GameObject. player.transform.SetParent(newParent); //Same as above, except this makes the player keep its local orientation rather than its global orientation. player.transform.SetParent(newParent, false); } }
SetSiblingIndex
类型:void SetSiblingIndex(int index)
参数:
index: 设置的索引
设置当前层级索引。
TransformDirection
类型:Vector3 TransformDirection(Vection direction)
把direction从自身坐标系转换到世界坐标系。
这个操作不受transform的缩放和位置影响。
返回的向量与direction长度相同。
如果向量代表的是一个位置而不是方向,使用Transform.TransformPoint。
2. public Vector3 TransformDirection(float x,
float y,
float z);
同上
TransformPoint
类型:Vector3 TransformPoint(Vector3 position)
把position从自身坐标系转换到世界坐标系
注意返回的位置受缩放影响。
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject someObject; public Vector3 thePosition; void Start() { // Instantiate an object to the right of the current object thePosition = transform.TransformPoint(Vector3.right * 2); Instantiate(someObject, thePosition, someObject.transform.rotation); } }
2. public Vector3 TransformPoint(float x,
float y,
float z);
同上
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject someObject; void Start() { // Instantiate an object to the right of the current object Vector3 thePosition = transform.TransformPoint(2, 0, 0); Instantiate(someObject, thePosition, someObject.transform.rotation); } }
TransformVector
类型: Vector3 TransformVector(Vector3 vector)
操作不受transform的位置影响,但是受缩放影响。返回的向量可能与vector长度不一样。
2. public Vector3 TransformVector(float x,
float y,
float z);
同上
Translate
类型:void Translate(Vector3 translation, Space relativeTo=Space.Self)
将transform以translation的方向和距离移动。
如果relativeTo没有设置或设置为Space.Self,移动是相对于自身坐标系。如果relativeTo设置为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); } }
2.public void Translate(float x,
float y,
float z, Space relativeTo =
Space.Self);
同上
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); } }
3.public void Translate(Vector3 translation, Transform relativeTo);
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform); } }
4.public void Translate(float x,
float y,
float z, Transform relativeTo);
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Update() { transform.Translate(Time.deltaTime, 0, 0, Camera.main.transform); } }
相关文章推荐
- 在字符串中找出第一个只出现一次的字符
- 大道至简第五章读后感
- 快门
- 当今app行业 比较流行的 简称 汇总
- eclipse快捷键
- virtual与abstract的区别
- 拆装箱及文件流
- 关于汇编-1
- git常用命令
- start oracle linux
- CentOS分区cfdisk
- 利用分支预测优化代码
- C语言中指针与数组的差别
- linux下不同服务器间数据传输
- 2015/11/1用Python写游戏,pygame入门(1):pygame的安装
- Android导入项目乱码的解决方法
- DataInputStream和DataOutputStream的简单使用
- ios 精简日历
- 学习数据挖掘资源
- UFT常见问题解决方案