对象的位置、旋转和缩放
该变换的子对象数量
print(transform.childCount);
所有子对象解除父子关系
transform.DetachChildren(); Destroy(gameObject);
此旋转作为欧拉角度
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); }
通过名字查找子对象并返回它
Transform aFinger; void Start() { aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger"); } void Update() { aFinger.Rotate(Time.deltaTime*20, 0, 0); }
在世界空间坐标,变换的蓝色轴。也就是z轴
rigidbody.velocity = transform.forward * 10;
通过索引返回一个变换的子对象
Transform GetChild(int index);
获取该对象的同级索引
void Start () { Debug.Log(this.transform.GetSiblingIndex()); }
此变换自从上次标识是否被设置为false
void OnUpdate() { if (transform.hasChanged) { print("The transform has changed!"); transform.hasChanged = false; } }
变换的方向从世界坐标转换到局部坐标。和Transform.TransformDirection相反
private Vector3 relative; void Example() { relative = transform.InverseTransformDirection(Vector3.forward); Debug.Log(relative); }
变换位置从世界坐标到局部坐标。和Transform.TransformPoint相反
public Transform cam = Camera.main.transform; public Vector3 cameraRelative = cam.InverseTransformPoint(transform.position); void Example() { if (cameraRelative.z > 0) print("The object is in front of the camera"); else print("The object is behind the camera"); }
变换一个向量从世界坐标空间到局部坐标空间。这个操作与Transform.TransformVector相反
public Vector3 Inverse TransformVector(Vector3 vector);
这个变换是parent的子对象?
void OnTriggerEnter(Collider col) { if (col.transform.IsChildOf(transform)) return; print("Do something here"); }
旋转作为欧拉角度,相对于父级的变换旋转
print(transform.localEulerAngles.x); print(transform.localEulerAngles.y); print(transform.localEulerAngles.z);
相对于父级的变换的位置
transform.localPosition = new Vector3(0, 0, 0); print(transform.localPosition.y);
该变换的旋转角度相对于父级变换的旋转角度
transform.localRotation = Quaternion.identity;
相对于父级变换的缩放
transform.localScale += new Vector3(0.1F, 0, 0);
变换点的矩阵从局部坐标到世界坐标
Matrix4x4 localToWorldMatrix;
旋转物体使z轴指向目标物体
public Transform target; void Update() { transform.LookAt(target); }
该对象的整体缩放
print(transform.lossyScale);
该变换的父对象
public Transform cameraTransform = Camera.main.transform; void Example() { cameraTransform.parent = transform; cameraTransform.localPosition = -Vector3.forward * 5; cameraTransform.LookAt(transform); }
在世界空间坐标transform的位置
transform.position = new Vector3(0, 0, 0); print(transform.position.x);
在世界坐标空间,变换的红色轴。也就是x轴
rigidbody.velocity = transform.right * 10;
返回最高层级的变换
void OnCollisionEnter(Collision collision) { if (collision.transform.root != transform.root) print("The colliding objects are not in the same hierarchy"); }
应用一个欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴
transform.Rotate(Vector3.right * Time.deltaTime); transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
围绕世界坐标的point点的axis旋转该变换angle度
transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
变换的旋转,在世界坐标空间储存为四元数
transform.rotation = Quaternion.identity;
移动该变换到此局部变换列表的开始
void SetAsFirstSibling();
移动该变换到此局部变换列表的末尾
void SetAsLastSibling();
设置该变换的父级
public Transform parent; void Start () { this.transform.SetParent(parent, false); }
设置同级对象的索引
void SetSiblingIndex(int index);
变换方向从局部坐标转换到世界坐标
public Transform cam = Camera.main.transform; public Vector3 cameraRelativeRight = cam.TransformDirection(Vector3.right); void Example() { rigidbody.AddForce(cameraRelativeRight * 10); }
变换位置从局部坐标到世界坐标
public GameObject someObject; public Vector3 thePosition = transform.TransformPoint(Vector3.right * 2); void Example() { Instantiate(someObject, thePosition, someObject.transform.rotation) as GameObject; }
变换一个向量从局部坐标空间到世界坐标空间
public Vector3 TransformVector(Vector3 vector);
向某方向移动物体多少距离
void Update() { transform.Translate(Vector3.forward * Time.deltaTime); transform.Translate(Vector3.up * Time.deltaTime, Space.World); }
在世界坐标空间,变换的绿色轴。也就是Y轴
rigidbody.velocity = transform.up * 10;
变换点的矩阵从世界坐标到局部坐标
Matrix4x4 worldToLocalMatrix;