Unity入门之重要组件和API(3) : Transform

前言

Transform类主要处理游戏对象(GameObject)的位移、旋转、缩放、父子关系和坐标转换。

1.位置和位移

1.1必备知识点:Vector3

Vector3 主要用来表示三维坐标系中的一个点或者一个向量。

【声明】
Vector3 v1 = new Vector3();
Vector3 v2 = new Vector3(10, 10);
Vector3 v3 = new Vector3(10, 10, 10);
Vector3 v4;

【Vector3 的基本计算:+ - * /】
print(v2 + v3);
print(v2 - v3);
print(v2 * 2);
print(v2 / 2);

【常用向量】
print(Vector3.zero);    // (0,0,0)
print(Vector3.right);   // (1,0,0)
print(Vector3.left);    // (-1,0,0)
print(Vector3.forward); // (0,0,1)
print(Vector3.back);    // (0,0,-1)
print(Vector3.up);      // (0,1,0)
print(Vector3.down);    // (0,-1,0)

【计算两个点之间的距离】
print(Vector3.Distance(v2, v3));

1.2位置

【相对世界坐标系】
print(this.transform.position);

【相对父对象】
print(this.transform.localPosition);

【注意:位置的赋值不能直接改变x, y, z, 只能整体改变】
this.transform.position = new Vector3(10, 10, 10);
this.transform.localPosition = new Vector3(20, 20, 20);

【对象当前的面朝向】
print(this.transform.forward);
【对象当前的头顶方向】
print(this.transform.up);
【对象当前的右手方向】
print(this.transform.right);

1.3位移

【位移计算公式:路程 = 方向 * 速度 * 时间】

【方式一:自己计算】
float speed = 1.0f;
[相对自己的方向移动]
this.transform.position += this.transform.forward * speed * Time.deltaTime;
[相对世界坐标方向移动]
this.transform.position += Vector3.forward * speed * Time.deltaTime;

【方式二:API】
参数一:位移量
参数二:参考坐标系
[1.相对于世界坐标系的 z轴 移动 (始终朝世界坐标系的z轴正方向移动)]
this.transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.World);
[2.相对于世界坐标系的 自己的面朝向 移动 (始终朝自己的面朝向移动)]
this.transform.Translate(transform.forward * speed * Time.deltaTime, Space.World);
[3.相对于自己坐标系的 z轴 移动 (始终朝自己的面朝向移动)]
this.transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
[4.相对于自己坐标系的 自己的面朝向 移动 (错误移动)]
//this.transform.Translate(transform.forward * speed * Time.deltaTime, Space.Self);

2.角度和旋转

2.1角度相关

【相对世界坐标系】
print(this.transform.eulerAngles);

【相对父对象】
print(this.transform.localEulerAngles);

【注意:角度的赋值不能直接改变x, y, z, 只能整体改变】
this.transform.eulerAngles = new Vector3(10, 10, 10);
this.transform.localEulerAngles = new Vector3(20, 20, 20);

2.2旋转相关

1.自转
参数一:旋转量
参数二:参考坐标系,默认是相对于自己坐标系进行旋转
this.transform.Rotate(new Vector3(0, 10, 0) * Time.deltaTime);
this.transform.Rotate(new Vector3(0, 10, 0) * Time.deltaTime, Space.World);

[相对于某个轴旋转]
参数一:相对哪个轴进行转动
参数二:旋转量
参数三:参考坐标系,默认是相对于自己坐标系进行旋转
this.transform.Rotate(Vector3.up, 10 * Time.deltaTime);
this.transform.Rotate(Vector3.right, 10 * Time.deltaTime, Space.World);

2.公转(相对于某个点旋转)
参数一:相对于哪个点旋转
参数二:相对于哪个轴旋转
参数三:旋转量
this.transform.RotateAround(Vector3.zero, Vector3.right, 10 * Time.deltaTime);

3.缩放和看向

3.1缩放

【相对世界坐标系】
print(this.transform.lossyScale);

【相对父对象】
print(this.transform.localScale);

【注意:位置的赋值不能直接改变x, y, z, 只能整体改变】
this.transform.localScale = new Vector3(2, 2, 2);

3.2看向

【看向一个点,相对于世界坐标系】
this.transform.LookAt(Vector3.zero);

【看向一个对象】
this.transform.LookAt(lookAtObj.transform);

4.父子关系

4.1获取和设置父对象

【获取父对象】
print(this.transform.parent.name);

【脱离父子关系】
this.transform.parent = null;
this.transform.SetParent(null);

【设置新的父对象】
this.transform.parent = GameObject.Find("Father").transform;
this.transform.SetParent(GameObject.Find("Father").transform);

4.2和所有子对象脱离关系

this.transform.DetachChildren();

4.3获取子对象

【按名字查找】
可以找到失活的对象,只能查找自己的子对象,不能查找子对象的子对象
this.transform.Find("Son");

【遍历子对象】
for (int i = 0; i < transform.childCount; i++) {
    print(transform.GetChild(i).name);
}

4.4子对象的操作

【判断某个对象是否自己的父对象】
if(transform.IsChildOf(GameObject.Find("Father").transform)) {
	print("Father 是父对象");
}

【得到自己作为子对象的编号】
print(transform.GetSiblingIndex());

【把自己设置为第一个子对象】
transform.SetAsFirstSibling();

【把自己设置为最后一个子对象】
transform.SetAsLastSibling();

【把自己设置为指定位置的子对象】
transform.SetSiblingIndex(1);

5.坐标转换

5.1世界坐标转本地坐标

【世界坐标系的点 转换为 本地坐标系的点】
受缩放影响
transform.InverseTransformPoint(Vector3.forward);

【世界坐标系的方向 转换为 本地坐标系的方向】
不受缩放影响
transform.InverseTransformDirection(Vector3.forward);
受缩放影响
transform.InverseTransformVector(Vector3.forward);

5.2本地坐标转世界坐标

【本地坐标系的点 转换为 世界坐标系的点】
受缩放影响
transform.TransformPoint(Vector3.forward);

【本地坐标系的方向 转换为 世界坐标系的方向】
不受缩放影响
transform.TransformDirection(Vector3.forward);
受缩放影响
transform.TransformVector(Vector3.forward);

  • 84
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值