Unity Transform

Transform

属性

属性含义
position世界空间位置vector3
localPosition在父对象中的相对位置vector3
eulerAngles世界空间的旋转,用欧拉角表示vector3
localEulerAngles在父对象空间中的旋转,用欧拉角表示vector3
right世界坐标系中的右方向(x轴)vector3
up世界坐标系中的上方向(y轴)vector3
forward世界坐标系中的前方向(z轴)vector3
rotation世界坐标系中的旋转 Quaternion
localRotation相对于父对象的局部旋转 Quaternion
lossyScale全局缩放(只读) vector3
localScale相对于父对象的局部缩放 vector3
worldToLocalMatrix世界坐标到对象局部坐标系的矩阵 Matrix4x4
localToWorldMatrix对象局部坐标系到世界坐标系的矩阵 Matrix4x4 = worldToLocalMatrix.inverse
root该对象所在层级关系的根节点对象 transform
childCount子节点数量 int
parent父节点 transform

接口

接口定义了一些操作transform的方法。

Translate

  • public void Translate(Vector3 translation);
  • public void Translate(Vector3 translation, Space relativeTo = Space.Self);

用带有方向和距离的 vector3移动对象:

// 向对象自己的前方移动。注意transform.forward是世界空间的,所以我们也要在世界空间移动才能匹配上
if (world)
{
    Vector3 move = transform.forward * distance;
    transform.Translate(move, Space.World);
}
// 在对象自己的空间里向前移动,需要将前方 变换到本地空间
else
{
    Vector3 localForward = transform.worldToLocalMatrix.MultiplyVector(transform.forward);
    Vector3 move = localForward * distance;
    transform.Translate(move, Space.Self);
}

InverseTransformDirection / TransformDirection

将一个代表方向的向量,从世界空间变换到对象本地空间。改变换将 忽略变换中的缩放,因为不均匀的缩放会改变方向。对于均匀缩放,变换后我们依然要对方向标准化(normalize),所以时没有意义的。

比如上面的例子:

Vector3 localForward = transform.worldToLocalMatrix.MultiplyVector(transform.forward);
//等价于下面的方法
localForward = transform.InverseTransformDirection(transform.forward);

transform.InverseTransformDirection其实是方便我们使用的接口,内部也是用worldToLocalMatrix进行变换的。

将本地空间的方向变换到世界空间

InverseTransformPoint / TransformPoint

将一个点,从世界空间变换到本地空间。变换将会应用缩放。

将本地空间的点变换到世界空间

InverseTransformVector / TransformVector

将一个vector3向量,从世界空间变换到局部空间。变换将会应用缩放。

将本地空间的vector变换到世界空间

LookAt

旋转transform,使对象“看向” target

Transform transform = Camera.main.transform;
Transform target;
// 让摄像机始终盯着目标
transform.LookAt(target);
// 跟上面一样,但是是向右歪着头看
transform.LookAt(Vector3.left);

Rotate

提供几种方法来旋转GameObject。

public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);

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

按照z->x->y轴的顺序来尽享旋转。Space.World沿着场景坐标系的轴旋转,Space.Local沿着对象自己的坐标系的轴旋转。

public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

沿着指定的任意axis轴,旋转angle度。axis可以是任意的方向。个人觉得改旋转是用四元数实现的:

// 经过验证,以下操作=transform.Rotate(axis,angle,Space.World),或者将axis变换到本地空间,再用Space.Selft
Quaternion q = Quaternion.AngleAxis(angle, axis);
transform.rotation = transform.rotation * q;

RotateAround

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

给定一个旋转轴和中心点,围绕该point,在axis轴上进行旋转。该操作会同时改变位置和朝向,就像你绕着地上的一个点转圈,只不过旋转轴是Vector3.up。

SetPositionAndRotation

public void SetPositionAndRotation(Vector3 position, Quaternion rotation);

很简单的接口,就是设置位置和旋转(朝向)。

Find

public Transform Find(string n);

根据给定的名字n,查找该transform的子节点。该查找不会递归访问其子节点。

如果名字n含有‘/’则会匹配节点路径上的节点。

GetChild

public Transform GetChild(int index);

获得索引号为 index 的子节点。index必须小于transform.childCount;

GetSiblingIndex

public int GetSiblingIndex();

获取自己在父节点的所有子节点中,自己排行第几:)

IsChildOf

public bool IsChildOf(Transform parent);

查询自己是不是某个节点的子节点。

返回 true:

  • 父节点
  • 爷爷节点(递归向上)
  • 就是自己,则返回true。

其它情况返回 false。

SetAsFirstSibling / SetAsLastSibling

public void SetAsFirstSibling();

public void SetAsLastSibling();

设置自己为父节点的子节点列表第一个(最后一个)节点。老大和老小。

SetSiblingIndex

public void SetSiblingIndex(int index);

设置自己排行第几。排行会在 Hierarchy 窗口中直接显示为层级

SetParent

public void SetParent(Transform p);

public void SetParent(Transform parent, bool worldPositionStays);

设置修改自己的父节点。

worldPositionStays表示保留自己在世界中的位置,旋转,缩放。如果传false,则保留对象的本地旋转平移缩放。

space

其中方位操作接口,要用到一个概念,Space,这是一个枚举,由2个值:

World:在Unity世界空间坐标系对Game Object 进行变换,且忽略Game Object本身的旋转状态。

Local:在对象自己的坐标系内进行变换,对象的旋转状态会影响变换结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值