Unity 入门 Transform类

来源:互联网 发布:搪瓷盘 知乎 编辑:程序博客网 时间:2024/04/28 03:33

1、Transform类中的常用方法


Transform.root 根
Transform.parent 父级
Transform.childCount 子物体数
Transform.SetParent();设置父节点

Transform.DetachChildren 分离子物体



(1)、FindChild(string name)

     方法类型:非静态。

     参数:name—要寻找该游戏对象的子对象的名称。

     返回值:Transform

     含义:通过名称来寻找该游戏对象下面的子对象。

(2)、GetChild(int index)

      方法类型:非静态。

      参数:index—要寻找该游戏对象的子对象的索引值。

      返回值:Transform

      含义:通过索引值来寻找该游戏对象下面的子对象。

(3)、GetChildCount()

      方法类型:非静态。

       参数:无。

       返回值:int

       含义:得到该游戏对象下面的子对象的个数。

(4)、LookAt(Transform target)

      方法类型:非静态。

      参数:target—要朝向的游戏对象。

       返回值:无。

      含义:使该游戏对象一直朝向目标游戏对象,即当目标游戏对象移动的时

            候,该游戏对象将旋转,保证一直朝向目标游戏对象。

 

应用示例:

//游戏对象保持朝向enemy对象

this.gameObject.transform.LookAt(enemy.transform);

(5)、Rotate(Vector3 eulerAngles, Space relativeTo)

     方法类型:非静态。

     参数:eulerAngles—要旋转的欧拉角,relativeTo—要选择的Space中的枚举值,其中有两个值Self(以自身坐标系为参考点运动)和World(以世界坐标系为参考点运动)。

     返回值:无。

     含义:游戏对象的根据欧拉角进行旋转,并且可以设置其参考的坐标系(世界坐标系或者自身坐标系)。

     重载:Rotate(Vector3 eulerAngles)。

     应用示例:

//游戏对象绕着Y轴旋转,并且以世界坐标系为参考

    this.gameObject.transform.Rotate(Vector3.up,Space.World);

(6)、RotateAround(Vector3 point, Vector3 axis,float angle)

      方法类型:非静态 。

      参数:point—游戏对象要绕着旋转的点;axis—游戏对象要绕着旋转的轴向;angle—旋转的速度。

      返回值:无。

      含义:游戏对象绕着某个点,以一个轴向(世界轴向),以某一速度进行旋转。

       重载:RotateAround(Vector3 axis, float angle)

     应用示例:

//游戏对象以sun对象为原点,以世界坐标的Y轴为轴向,以speed的速度旋转

this.gameObject.transform.RotateAround(sun.transform.position,Vector3.up,speed);

(7)、Translate(Vector3 translation, SpacerelativeTo)

      方法类型:非静态 。

      参数:translation—游戏对象移动的方向;relativeTo—要选择的Space中的枚举值,其中有两个值Self(以自身坐标系为参考点运动)和World(以世界坐标系为参考点运动)。

       返回值:无。

       含义:游戏对象按照某个方向移动,并且可以设置其参考的坐标系(世界坐标系或者自身坐标系)。

 应用示例;

//游戏对象以世界坐标Y轴为方向,以世界坐标为参考,进行移动。

this.gameObject.transform.Translate(Vector3.up*Time.deltaTime,Space.World);

 

 

 

 

3、经验总结

(1)、控制游戏对象的移动

      用Translate方法控制:

     //Translate方法控制我们游戏对象的移动  方向为Y轴方向

      this.gameObject.transform.Translate(Vector3.up,Space.Self);

      用position来控制:

//通过改变position的位置来控制游戏对象的移动,方向为世界坐标的Y轴

      this.gameObject.transform.position += Vector3.up;

两者的不同点:

用Translate方法控制的游戏对象的移动,可以设置其方向为世界坐标的方向还是自己坐标的方向,但是用position来控制游戏队形的移动,其方向只能是世界坐标的方向。

(2)、控制游戏对象的缩放信息

   //通过localScale来控制游戏对象的不断放大

this.gameObject.transform.localScale += new Vector3(1,1,1);

//通过localScale来控制游戏对象的一次放大

this.gameObject.transform.localScale= new Vector3(2,2,2);

(3)、控制游戏对象的旋转

//通过eulerAngles(欧拉角)来控制游戏对象不断的旋转一个角度

this.gameObject.transform.eulerAngles+=newVector3(45,0,0);

//通过eulerAngles(欧拉角)来控制游戏对象只旋转一个角度

this.gameObject.transform.eulerAngles=newVector3(45,0,0);

//通过Ratate来控制 游戏对象以自身参考旋转,不断的旋转,

this.gameObject.transform.Rotate(new Vector3(45,0,0),Space.self);

 


0 0
原创粉丝点击