【Unity】入门学习笔记180524——API(17)——Transform类——实例方法

B、Transform类实例方法

1、DetachChildren:分离物体层级关系

public void DetachChildren();

此方法的功能是使GameObject对象的所有子物体和自身分离层级关系


2、GetChild:获取GameObject对象子类

public Transform GetChild(int index);

index为子物体索引值

此方法用于返回transform的索引值为index的子类Transform实例

参数index的值要小于transform的childCount值


3、InverseTransformDirection:坐标系转换

public Vector3 InverseTransformDirection( Vector3 direction );

public Vector3 InverseTransformDirection( float x, float y, float z);

将参数direction从世界坐标系转换到GameObject对象的局部坐标系


4、InverseTransformPoint:点的相对坐标向量

public Vector3 InverseTransformPoint(Vector3 position);

public Vector3 InverseTransformPoint(float x, float y, float z);

用于返回参数position向量相对于GameObject对象局部坐标系的差向量,即返回向量position和向量transform.position的差值


5、IsChildOf:是否为子物体

public bool IsChildOf( Transform patent );

用于判断transform对应的GameObject对象是否为参数parent的子物体

返回值为true的三种情况:

A.isChildOf(B)

①A和B指向同一对象

②A是B的一级子物体

③A是B的多级子物体


6、LookAt:物体朝向

public void LookAt(Transform target);

public void LookAt(Vector3 worldPosition);

public void LookAt(Transform target, Vector3 worldup);

public void LookAt(Vector3 worldPosition, Vector3 worldup);

其中参数target为transform自身坐标系中z轴指向的目标

参数worldup为transform自身坐标系中y轴最大限度指向的方向

此方法的功能是使得GameObject对象自身坐标系中的z轴指向target,y轴方向最大限度地指向worldup方向


7、Rotate:绕坐标轴旋转

public void Rotate(Vector3 eulerAngles);

public void Rotate(Vector3 eulerAngles, Space relativeTo);

public void Rotate(float xAngle, float yAngle, float zAngle);

public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);

其中eulerAngles为transform要旋转地欧拉角,参数relativeTo为transform旋转时参考的坐标系,默认为Space.self

此方法的功能是使得transfor实例在相对参数relativeTo的坐标系中旋转欧拉角eulerAngles


8、Rotate:绕某个向量旋转

public void Rotate(Vector3 axis, float angle);

public void Rotate(Vecotr3 axis, float angle, Space relativeTo);

其中参数axis为旋转轴方向,参数angle为旋转角度,参数relativeTo为参数坐标系,默认为space.self

此方法的功能是使得GameObject对象在relativeTo坐标系中绕轴向向量axis旋转angle度

若想使GameObject对象实例绕着某个物体旋转,请用Transform.RotateAround(point:Vector3,axis:Vector3,angle:float)方法


9、RotateAround:绕轴点旋转

public void RotateAround(Vector3 axis, float angle);

public void RotateAround(Vector3 point, Vector3 axis, float angle);

其中参数point为参考点坐标,参数axis为旋转轴方向,参数angle为旋转角度

此方法的功能使使得GameObject对象绕着point点的axis方向旋转angle度


10、TransformDirection:坐标系转换

public Vector3 TransformDirection(Vector3 direction);

参数direction为待转换的Vector3实例向量

public Vector3 TransformDirection(float x, float y, float z);

此方法用于将向量direction从transform局部坐标系转换到世界坐标系


11、TransformPoint:点的世界坐标位置

public Vector3 TransformPoint(Vector3 position);

其中参数position为transform局部坐标系的向量

public Vector3 TransformPoint(float x, float y, float z);

此方法用于返回GameObject对象局部坐标系中向量position在世界坐标系中的位置


12、Translate:相对坐标系移动

public void Translate(Vector3 translation);

public void Translate(Vector3 translation, Space relativeTo);

public void Translate(float x, float y, float z);

public void Translate(float x, float y, float z, Space relativeTo);

其中参数translation为移动向量,包括方向和大小,参数relativeTo为参考坐标系空间,默认为Space.Self

此方法的功能使使得GameObject对象在参数relativeTo的坐标系空间中移动参数translaton指定的向量


13、Translate:相对其他物体移动

public void Translate(Vector3 translation, Transform relativeTo); 

public void Translate(float x, float y, float z, transform relativeTo);

其中参数translation为移动向量,包括方向和大小,参数relatievTo为移动参考物体,默认为Space.World

此方法的功能是使得GameObject对象在相对relativeTo的坐标系中移动向量translation

猜你喜欢

转载自blog.csdn.net/dylan_day/article/details/80433084
今日推荐