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

前言

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

1.位置和位移

1.1必备知识点:Vector3

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

cs 复制代码
【声明】
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位置

cs 复制代码
【相对世界坐标系】
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位移

cs 复制代码
【位移计算公式:路程 = 方向 * 速度 * 时间】

【方式一:自己计算】
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角度相关

cs 复制代码
【相对世界坐标系】
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旋转相关

cs 复制代码
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缩放

cs 复制代码
【相对世界坐标系】
print(this.transform.lossyScale);

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

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

3.2看向

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

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

4.父子关系

4.1获取和设置父对象

cs 复制代码
【获取父对象】
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和所有子对象脱离关系

cs 复制代码
this.transform.DetachChildren();

4.3获取子对象

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

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

4.4子对象的操作

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

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

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

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

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

5.坐标转换

5.1世界坐标转本地坐标

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

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

5.2本地坐标转世界坐标

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

【本地坐标系的方向 转换为 世界坐标系的方向】
不受缩放影响
transform.TransformDirection(Vector3.forward);
受缩放影响
transform.TransformVector(Vector3.forward);
相关推荐
胜天半子_王二_王半仙3 小时前
godot源码编译
游戏引擎·godot
Thomas_YXQ3 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
o0向阳而生0o3 小时前
28、.NET 中元数据是什么?
microsoft·c#·.net
Tandy12356_4 小时前
Godot开发2D冒险游戏——第二节:主角光环整起来!
游戏引擎·godot
niuTaylor4 小时前
Linux驱动开发快速上手指南:从理论到实战
linux·运维·开发语言·驱动开发·c#
军训猫猫头4 小时前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
冰茶_6 小时前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
The Future is mine7 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
星火撩猿11 小时前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot
sky_smile_Allen12 小时前
[Unity]-[UI]-[Prefab] 关于Unity UGUI 的布局及组件讲解
ui·unity·游戏引擎