【Unity3D】Transform组件

打印 上一主题 下一主题

主题 629|帖子 629|积分 1887

1 前言

        每个游戏对象有且仅有一个 Transform 组件,Transform 组件保存了游戏对象的位置信息,用户可以通过操作 Transform 组件实现对游戏对象的平移、旋转、缩放等变换。每个 Script 组件需要继承 MonoBehaviour,MonoBehaviour 中有 Transform 属性。
        如果读者对 MonoBehaviour 不太了解,可以参考→MonoBehaviour的生命周期
        1)获取当前游戏对象的 Transform 组件
  1. this.transform;
  2. gameObject.tramsform;
  3. GetComponent<Transform>();
  4. gameObject.GetComponent<Transform>();
复制代码
        2)获取其他游戏对象的 Transform 组件
  1. GameObject.Find("name").transform
  2. GameObject.FindGameObjectWithTag("tag").transform
  3. transform.FindChild("name");
复制代码
       3)Transform 中属性
  1. // 游戏对象
  2. gameObject
  3. // name 和 tag
  4. name、tag
  5. // 游戏对象的位置
  6. position、localPosition
  7. // 游戏对象的旋转角
  8. eulerAngles、localEulerAngles、rotation、localRotation
  9. // 游戏对象的缩放比
  10. localScale
  11. // 游戏对象的右方、上方、前方
  12. right、up、forward
  13. // 层级相关
  14. root、parent、hierarchyCount、childCount
  15. // 相机和灯光
  16. camera、light
  17. // 游戏对象的其他组件
  18. animation、audio、collider、renderer、rigidbody
复制代码
        说明:rotation 属性是一个四元数(Quaternion)对象,可以通过如下方式将向量转换为 Quaternion:
  1. Vector3 forward = new Vector3(1, 1, 1);
  2. Quaternion quaternion = Quaternion.LookRotation(forward);
复制代码
        4)Transform 中方法
  1. // 平移
  2. Translate(Vector3 translation)
  3. // 绕eulerAngles轴自转,自转角度等于eulerAngles的模长
  4. Rotate(Vector3 eulerAngles)
  5. // 绕过point点的axis方向的轴公转angle度
  6. RotateAround(Vector3 point, Vector3 axis, float angle)
  7. // 绕过point点的axis方向的轴公转angle度(坐标系采用本地坐标系)
  8. RotateAroundLocal(Vector3 point, Vector3 axis, float angle)
  9. // 看向transform组件的位置
  10. LookAt(transform)
  11. // 获取组件类型
  12. GetType()
  13. // 获取子游戏对象的Transform组件(不能获取孙子组件)
  14. FindChild("name")
  15. // 获取子游戏对象的Transform组件(可以获取孙子组件)
  16. Find("name")
  17. // 通过索引获取子游戏对象的Transform组件(不能获取孙子组件)
  18. GetChild(index)
  19. // 获取游戏对象的子对象个数
  20. GetChildCount()
  21. // 获取其他组件
  22. GetComponent<T>
  23. // 在子游戏对象中获取组件
  24. GetComponentsInChildren<T>()
复制代码
2 应用

        本节将使用 Transform 组件实现月球绕地球转、地球绕太阳转,同时太阳、地球、月球都在自转。
        首先通过【GameObject→3D Object→Sphere】创建3个球,分别代表太阳、地球、月球,调整位置坐标分别为:(0, 0, 0)、(2, 0, 0)、(3, 0, 0),调整缩放系数分别为:1、0.7、0.5,再将如下 3 张图片拖至对应星球上。

        拖拽结束后,会生成 Materials 文件夹,里面有 3 个材质球,如下:

         接着通过【GameObject→3D Object→Plane】创建 1 个 Plane 对象,重命名为 “bg”,在 Materials 文件夹里右键,选择【Create→Material】创建一个材质球,选中材质球,在 Inspector 窗口给材质球添加黑色,再将该材质球拖拽到 bg 游戏对象上。

        在 Assets 窗口右键,选择【Create→C# Script】创建 3 个 Script 组件,分别重命名为 Sun、Earth、Moon,将 3 个 Script 组件分别拖拽至对应星球上。
        Sun.cs
  1. using UnityEngine;
  2. public class Sun : MonoBehaviour {
  3.         void Start () {
  4.         }
  5.         void Update () {
  6.                 transform.Rotate(-2 * Vector3.up);
  7.         }
  8. }
复制代码
        Earth.cs
  1. using UnityEngine;
  2. public class Earth : MonoBehaviour {
  3.         private Transform center;
  4.         void Start () {
  5.                 center = GameObject.FindGameObjectWithTag("Sun").transform;
  6.         }
  7.         void Update () {
  8.                 transform.RotateAround(center.position, -Vector3.up, 2);
  9.                 transform.Rotate(-4 * Vector3.up);
  10.         }
  11. }
复制代码
        Moon.cs
  1. using UnityEngine;
  2. public class Moon : MonoBehaviour {
  3.         private Transform center;
  4.         private float theta = 0f;
  5.         void Start () {
  6.                 center = GameObject.FindGameObjectWithTag("Earth").transform;
  7.         }
  8.         void Update () {
  9.                 theta = theta + 0.08f;
  10.                 transform.position = new Vector3(center.position.x + Mathf.Cos(theta), 0f, center.position.z + Mathf.Sin(theta));
  11.                 transform.Rotate(-3 * Vector3.up);
  12.         }
  13. }
复制代码
        运行效果



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

反转基因福娃

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表