许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  【Unity3D】Transform组件

【Unity3D】Transform组件

阅读数 5
点赞 0
article_banner

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>();
cs
运行

        2)获取其他游戏对象的 Transform 组件

  1. GameObject.Find("name").transform
  2. GameObject.FindGameObjectWithTag("tag").transform
  3. transform.FindChild("name");
cs
运行

       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
cs
运行

        说明:rotation 属性是一个四元数(Quaternion)对象,可以通过如下方式将向量转换为 Quaternion:

  1. Vector3 forward = new Vector3(1, 1, 1);
  2. Quaternion quaternion = Quaternion.LookRotation(forward);
cs
运行

        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>()
cs
运行

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. }
cs
运行

        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. }
cs
运行

        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. }
cs
运行

        运行效果


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空