Unity Transform 学习

一、成员变量
position:在世界空间坐标transform的位置。
localPosition:相对于父级的变换的位置。如果该变换没有父级,那么等同于Transform.position。
eulerAngles:世界坐标系中的旋转(欧拉角)。
localEulerAngles:相对于父级的变换旋转角度。
right:世界坐标系中的右方向。(世界空间坐标变换的红色轴。也就是x轴。)
up:世界坐标系中的上方向。(在世界空间坐标变换的绿色轴。也就是y轴。)
forward:世界坐标系中的前方向。(在世界空间坐标变换的蓝色轴。也就是z轴。)
rotation:世界坐标系中的旋转(四元数)。
localRotation:相对于父级的变换旋转角度。
localScale:相对于父级的缩放比例。
parent:父对象Transform组件。
worldToLocalMatrix:矩阵变换的点从世界坐标转为自身坐标(只读)。
localToWorldMatrix:矩阵变换的点从自身坐标转为世界坐标(只读)。
root:对象层级关系中的根对象的Transform组件。
childCount:子对象数量。
lossyScale:全局缩放比例(只读)。
 

 二、函数

LookAt 函数

1、使游戏对象看向 worldPosition 坐标点

public void LookAt(Vector3 worldPosition);  

2、使游戏对象看向 target 的坐标点,即 target.position

public void LookAt(Transform target);

代码示例

public class TestScript : MonoBehaviour
{
    public Transform m_camars;
    public Transform m_cube;

    void OnDrawGizmos()
    {
        // 画一条红线,从方块到摄像机
        Gizmos.color = Color.red;
        Gizmos.DrawLine(m_cube.position, m_camars.position);

        // 摄像机看向方块
        m_camars.LookAt(m_cube);
    }
}

Translate 函数

1、把物体向translation方向移动,移动距离为translation.magnitude, relativeTo表示这个移动的参考坐标系Space.Self 自身坐标系,Space.World世界坐标系。

public void Translate (Vector3 translation , Space relativeTo = Space.Self) 
把物体向translation方向移动,距离为translation.magnitude。 relativeTo表示这个移动的参考坐标系。

2、把物体向(x, y, z)方向移动, relativeTo表示这个移动的参考坐标系Space.Self 自身坐标系,Space.World世界坐标系。

public void Translate (float x, float y, float z, Space relativeTo = Space.Self) 

3、把物体向translation方向移动,移动距离为translation.magnitude,如果relativeTo 不是null,则以目标物体relativeTo的自身轴向作为参考坐标系。 

public void Translate (Vector3 translation, Transform relativeTo) 

4、把物体向(x, y, z)方向移动,如果relativeTo 不是null,则以目标物体relativeTo的自身轴向作为参考坐标系。 

public void Translate (float x, float y, float z, Transform relativeTo)

代码示例

    void OnDrawGizmos()
    {
        // 自身坐标系中点(4, 3, 2)在世界坐标系中的位置
        Vector3 pos = m_cube0.TransformPoint(new Vector3(4, 3, 2));

        Gizmos.color = Color.green;
        Gizmos.DrawLine(m_cube0.position, pos);

        if (m_isMove)
        {
            m_isMove = false;

            // 方块移动到dist,以自身坐标系为参考
            m_cube1.Translate(new Vector3(4, 3, 2), Space.Self);
        }
    }

Rotate 函数

用来旋转物体的函数,非常常用,在知道需要旋转的角度的情况下。如果要让物体旋转到指定位置,需要搭配Quaternion来使用。

1、以relativeTo为参考坐标系,旋转eulerAngles度(3个轴向分别旋转),在原有的角度上累加

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

2、以relativeTo为参考坐标系,3个轴向分别旋转,在原有的角度上累加

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

3、以relativeTo为参考坐标系,以axis为轴旋转angle度

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

代码示例

    void OnDrawGizmos()
    {
        if (m_isMove)
        {
            m_isMove = false;

            // 绕自身y轴旋转45°
            m_cube0.Rotate(new Vector3(0, 45, 0), Space.Self);

            // 绕自身y轴旋转45°
            m_cube1.Rotate(0, 45, 0, Space.Self);

            // 绕自身y轴旋转45°
            m_cube2.Rotate(m_cube2.up, 45, Space.Self);
        }
    }

RotateAround 函数

让物体以某一点为轴心成圆周运动。

1、public void RotateAround (Vector3 point, Vector3 axis, float angle) 

以point点为基准 绕axis轴旋转一个角度angle。point是世界坐标系,axis的坐标系是世界坐标系,angle的单位是 弧度。

2、public void RotateAround (Vector3 axis, float angle) 

以自身中心点为基准 绕axis轴旋转一个角度angle。axis的坐标系是世界坐标系,angle的单位是 弧度。

代码示例

   void OnDrawGizmos()
    {
        if (m_isMove)
        {
            m_isMove = false;

            // 记录点
            m_points.Add(new Vector3(m_cube0.position.x, m_cube0.position.y, m_cube0.position.z));

            // 轴心
            Vector3 point = new Vector3(3, 0, 4);

            // 轴
            Vector3 axis = new Vector3(0, 1, 0);

            // 旋转45°
            m_cube0.RotateAround(point, axis, 45);

            // 记录点
            m_points.Add(new Vector3(m_cube0.position.x, m_cube0.position.y, m_cube0.position.z));
        }

        // 绘制线
        for(int i = 0;i<m_points.Count;i++)
        {
            Gizmos.DrawLine(m_points[i], new Vector3(3, 0, 4));
        }
    }

RotateAroundLocal 函数

1、public void RotateAroundLocal(Vector3 axis, float angle);

以自身中心点为基准 绕axis轴旋转一个角度angle。axis的坐标系是世界坐标系,angle的单位是 弧度。

TransformDirection 函数

返回以物体自身为坐标轴的向量direction(x, y, z)在世界坐标中的朝向向量。

1、public Vector3 TransformDirection (Vector3 direction)

2、public Vector3 TransformDirection (float x, float y, float z)

代码示例

// 位置设置为(1, 1, 1)
m_cube0.position = new Vector3(1, 1, 1);

// 旋转设置为(45, 45, 45)
m_cube0.eulerAngles = new Vector3(45, 45, 45);

// 自身y轴
Debug.Log("自身y轴向量: " + m_cube0.up);

// 自身y轴在世界坐标中的朝向向量
Debug.Log("自身y轴向量在世界坐标中的朝向向量: " + m_cube0.TransformDirection(m_cube0.up));

InverseTransformDirection 函数

与TransformDirection相反,从世界坐标转换到自身相对坐标。

1、public Vector3 InverseTransformDirectionTransformDirection (Vector3 direction) 

2、public Vector3 InverseTransformDirectionTransformDirection (float x, float y, float z) 

TransformPoint 函数

把一个点从自身相对坐标转换到世界坐标。

1、public Vector3 TransformPoint (Vector3 position)

2、public Vector3 TransformPoint (float x, float y, float z) 

InverseTransformPoint 函数

把一个点从世界坐标转换到自身坐标的位置。

1、public Vector3  InverseTransformPoint (Vector3 position)

2、public Vector3  InverseTransformPoint (float x , float y, float z)

DetachChildren 函数

把自身所有的子物体的父物体都设成世界,也就是跟自己的所有子物体结束父子关系。

1、public void DetachChildren ()

Find 函数

找到一个名字是name的物体并返回

如果没有找到则返回null。如果字符串被/隔离,函数则会像文件路径一样逐级下查。

1、public Transform Find (string name) 

// The magical rotating finger
function Update() {
        aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
        aFinger.Rotate(Time.deltaTime*20, 0, 0);
}

IsChildOf 函数

1、public bool IsChildOf (Transform parent) 
如果物体是parent的父子层级关系下的一员,返回true;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值