Transform组件基础
Transform是Unity中所有GameObject必备的组件,用于控制对象的位置、旋转和缩放。每个GameObject在场景中都有且只有一个Transform组件。
位置和位移
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson6 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#region 知识点一 Vector3基础
//Vector主要是用来表示三维坐标系中的一个点 或者一个向量
//申明
Vector3 v = new Vector3();
v.x = 10;
v.y = 10;
v.z = 10;
//只传x,y,z默认是0
Vector3 v2 = new Vector3(10, 10);
//一步到位
Vector3 v3 = new Vector3(10, 10, 10);
//vector的基本运算
print(v2- v3);
//和正常的+-*/方法一样
//常用
print(Vector3 .zero);//0 0 0
print(Vector3 .right );//1 0 0
print(Vector3.left);//-1 0 0
print(Vector3 .up );//0 1 0
print(Vector3 .forward );//0 0 1
print(Vector3 .back );//0 0 -1
print (Vector3 .down );//0 -1 0
print (Vector3 .one );//单位向量
//计算两个点之间距离的方法
Vector3.Distance(v2, v3);
#endregion
#region 知识点二位置
//相对世界坐标系
print(this.transform .position);
//通过position得到的位置 是相对于世界坐标系原点的位置
//可能和面板上显示的是不一样的
//因为如果有父子关系 并且父对象的位置不在原点,那么和面板上的肯定不一样
//相对于父对象
print(this.transform .localPosition );
//如果想以面板坐标为准,那就用localPosition
//相对世界坐标和相对父对象坐标可能一样的情况
//1.父对象的坐标就是世界原点
//2.对象没有父对象
//注意:位置的赋值不能直接改变x,y,z 只能整体改变
this.transform.position=new Vector3(10,10,10);
//如果只想改变x, y,z保持不变
//1.直接赋值
this.transform.position = new Vector3(19, this.transform.position.y, this.transform.position.z);
//2.先取出来,再赋值
//虽然不能直接改transform的x,y,z值,但是可以直接改Vector的x,y,z值
//所以 可以先取出来改Vector再改
Vector3 Vpos = this.transform.position;
Vpos.x = 13;
this.transform.position = Vpos;
//对象当前的各个朝向
print(this.transform.forward);//对象当前的面朝向
print(this.transform.up);//对象当前的头顶朝向
print(this.transform.right);//对象当前的右手朝向
//如果你想得到对象当前的一个朝向
//那么就通过transform.出来的
#endregion
}
// Update is called once per frame
void Update()
{
#region 知识点三 位移
//理解坐标系下的位移计算公式
//路程=方向*速度*时间
//方式一 自己计算
//当前的位置+移动的距离=最终的位置
//this.transform.position += this.transform.forward * 1 * Time.deltaTime;
//方向非常的重要,他直接决定了你的移动方向
//沿世界坐标系方向进行移动
//this.transform.position += Vector3.forward * 1 * Time.deltaTime;
//方式二 API
//参数一:表示位移的多少
//参数二:表示相对坐标系 默认是相对于自己坐标系的
//相对于世界坐标系的z轴移动
//this.transform .Translate (Vector3 .forward *1*Time .deltaTime,Space.World);
//相对于世界坐标的 自己的面朝向去移动
this.transform.Translate(this.transform.forward*1*Time .deltaTime,Space.World);
//相对于自己坐标系 自己的面朝向移动(一定不会这样让物体移动)
this.transform.Translate(this.transform.forward * 1 * Time.deltaTime, Space.Self);
//相对于自己坐标系 Z轴朝向移动
this.transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.Self );
//注意:一般使用API来位移
#endregion
}
}
角度和旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson7 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#region 知识点一 角度相关
//相对于世界坐标角度
print(this.transform.eulerAngles);
//相对于父对象的角度
print(this.transform .localEulerAngles );
//注意:设置角度和设置位置一样,不能单独设置x,y,z
//如果希望改变的角度是面板上显示的内容 那就要改变相对于父对象的角度
#endregion
}
// Update is called once per frame
void Update()
{
#region 知识点二 旋转相关
//自己计算(和位置一样)
//API
//自转
//每个轴具体转多少度
//第一个参数 相当于是旋转的角度 每一帧
//第二个参数 默认不填 就是相对于自己坐标系 进行的旋转
//this.transform.Rotate(new Vector3(0, 10, 0)*10 * Time.deltaTime);
//this.transform.Rotate(new Vector3(0, 10, 0)*10 * Time.deltaTime,Space.World);
//相对于某个轴
//参数一:是相对于哪个轴转
//参数二:转动的角度
//参数三:默认不填 是相对于自己坐标系旋转
//this.transform .Rotate (Vector3 .up ,10*Time. deltaTime);
//相对于某一个点转
//1.哪个点
//2.相对于这个点哪个轴
//3.转的度数
this.transform .RotateAround (Vector3 .zero ,Vector3 .up ,10*Time .deltaTime );
#endregion
}
}
缩放和看向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson8 : MonoBehaviour
{
// Start is called before the first frame update
public Transform obj;
void Start()
{
#region 缩放
//相对于世界坐标系
print(this.transform .lossyScale );
//相对于本地坐标系(父对象)
print(this.transform .localScale );
//注意:同样只能一起改
//相对于世界坐标系的缩放大小只能得到,不能改
//Unity没有提供关于缩放的API
#endregion
#region 看向
//让一个对象面朝向 可以一直看向某一个点或某一个对象
//this.transform .LookAt (Vector3 .zero);
//看向一个对象
transform.LookAt(obj);
#endregion
}
// Update is called once per frame
void Update()
{
//自己算
//this.transform.localScale += Vector3.one * Time.deltaTime;
transform.LookAt(obj);
}
}
父子关系
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson9 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#region 获取和设置父对象
//获取父对象
print(this.transform.parent);
//断绝父子关系
this.transform.parent = null;
//认父亲
this.transform.parent = GameObject.Find("Father").transform;
//API
this.transform.SetParent(null);
//第二个参数:填true,保留世界坐标系下的状态,和父对象进行计算 得到本地坐标系的信息
//false:不会保留,会直接把世界坐标系下的 位置角度 缩放 直接复制到本地坐标系下
this.transform.SetParent(null,true);
#endregion
#region 抛妻弃子
//断绝父子关系
this.transform.DetachChildren();
#endregion
#region 获取子对象
//找儿子
this.transform.Find("儿子名");
//可以找到失活的对象
//只能找到儿子,找不到孙子
//遍历儿子
for (int i = 0; i < transform.childCount ; i++)
{
print(transform.GetChild(i));
}
#endregion
#region 儿子的操作
//判断自己爸爸是谁
this.transform.IsChildOf(this.transform);
//得到自己作为儿子的编号
this.transform.GetSiblingIndex();
//把自己设置为第一个儿子
this.transform.SetAsFirstSibling();
//把自己设置为最后一个儿子
this.transform.SetAsLastSibling();
//把自己设置为指定儿子
this.transform.SetSiblingIndex(7);
//就算你填的数字超出了范围,就把你设置成最后一个儿子编号
#endregion
#region 练习题一
//给Transform写一个拓展方法,实现将他的子对象按名字长短排序改变他们的顺序
//名字短的在前
#endregion
#region 练习题二
//为Transform写一个扩展方法,传入一个名字查找他的子对象,以及子对象的子对象
#endregion
}
// Update is called once per frame
void Update()
{
}
}
练习题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Tools
{
public static void Sort(this Transform obj)
{
List<Transform> list = new List<Transform>();
for (int i = 0; i < obj.childCount ; i++)
{
list.Add (obj.GetChild(i));
}
list.Sort((a, b) =>
{
if (a.name.Length < b.name.Length)
return -1;
return 1;
});
for (int i = 0; i < list.Count ; i++)
{
list[i].SetSiblingIndex(i);
}
}
public static Transform CustomFind(this Transform father,string childName)
{
Transform target = null;
target = father.Find(childName);
if (target != null)
return target;
for (int i = 0; i < father .childCount ; i++)
{
//递归
target =father.GetChild(i).CustomFind(childName);
if(target != null) return target;
}
return target;
}
}
坐标转换
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson10 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#region 知识点一 世界坐标转本地坐标
print(Vector3.forward);
//世界坐标系 转本地坐标系 可以帮助我们大概判断一个相对位置
//世界坐标系的点 转换 为相对本地坐标系的点
//受到缩放影响
print("转换后的点 " + this.transform.InverseTransformPoint(Vector3.forward));
//世界坐标系的方向 转换 为相对本地坐标系的方向
//不受缩放影响
print("转换后的方向" + this.transform.InverseTransformDirection(Vector3.forward));
//受缩放影响
print("转换后的方向(受缩放影响)" + this.transform.InverseTransformVector(Vector3.forward));
#endregion
#region 知识点二 本地坐标转世界坐标
//本地坐标系的点 转换 为相对世界坐标系的点 受到缩放影响
print("本地 转 世界 点" + this.transform.TransformPoint(Vector3.forward));
//本地坐标系的方向 转换 为相对世界坐标系的方向
//不受缩放影响
print("本地 转 世界 方向" + this.transform.TransformDirection(Vector3.forward));
//受缩放影响
print("本地 转 世界 方向" + this.transform.TransformVector(Vector3.forward));
#endregion
#region 练习题
//一个物体A,不管他在什么位置,写一个方法,只要执行这个方法就在他面前创建三个物体
//位置是(0,0,1)(0,0,2)(0,0,3)
#endregion
}
[ContextMenu("面前创建三个球体")]
void Fun()
{
for (int i = 1; i <= 3; i++)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = this.transform.TransformPoint(Vector3.forward * i);
}
}
// Update is called once per frame
void Update()
{
}
}

3639

被折叠的 条评论
为什么被折叠?



