关于Unity中的transform组件(三)

game_root节点下右一个Cube子节点,和一个Sphere节点,脚本挂载在game_root下

四元数:(1)Quaternion rot 

    (2)this.cube.rotation

欧拉角:Vector3类型的数据

Vector3的x,y,z是按照Unity的坐标系的顺序,所以不能像数学里面,把Unity的z当成了y,Unity的y当成了z。

 

一、使用欧拉角做旋转(欧拉角)

 private Transform cube;

void Start () {

  this.cube = this.transform.FindChild("Cube");//找到子节点叫Cube的节点的transform组件  

  this.cube.Rotate(new Vector3(0, 45, 0));//旋转45度,逆时针为正,Rotate里面还可以加一个参数,表示绕谁旋转,Space_Self,Space_World,默认是绕自己坐标系旋转。

}

 

 

二、使用四元数做旋转(旋转方向向量,角度),其实这里没有用到四元数来旋转,只是一个方法,四元数是一个虚数,很复杂。

 private Transform cube;

void Start () {

  this.cube = this.transform.FindChild("Cube");//找到子节点叫Cube的节点的transform组件

  // 旋转向量 + 角度
  this.cube.Rotate(new Vector3(0, 1, 0), 45);//旋转45度,逆时针为正,new Vector3(0, 1, 0)表示绕y轴旋转,Rotate里面还可以加一个参数,表示绕谁旋转,Space_Self,Space_World,默认是绕自己坐标系旋转。

}

 

 

三、绕着一个物体旋转

void Update () {

  float w = 180; // 定义一个旋转速度
  Vector3 pos = new Vector3(0, 0, 0);//定义围绕旋转的目标点
  this.sphere.RotateAround(pos, new Vector3(0, 1, 0), Time.deltaTime * w);//绕着世界(0,0,0)点,绕目标y轴,每一次刷新旋转Time.deltaTime * w的角度

}

 

 

四、盯着一个物体

1.LootAt(): 视线对准对应的方位。像寻路,NAV导航的时候会用到,游戏跟随会用到。

void Update () {

  // LookAt有几个参数

  // (1)旋转角度,只有给出盯着的目标this.sphere,或者盯着的目标的位置this.sphere.position就会得到每次的旋转角度
  // (2)头顶方向,旋转时候饶的这个向量,默认是y轴
  this.cube.LookAt(this.sphere, new Vector3(0, 1, 0));

}

 

2.LookRotation():来自四元数的方法,视线对准对应的方位。

void Update () {

  // LookRotation有几个参数

  // (1)旋转角度,只有给出盯着的目标this.sphere,或者盯着的目标的位置this.sphere.position就会得到每次的旋转角度
  // (2)头顶方向,旋转时候饶的这个向量,默认是y轴

  Quaternion rot = Quaternion.LookRotation(this.sphere.position);
  this.cube.rotation = rot;

}

 

五、用四元数计算两个旋转之间的夹角;

Angle(lhs, rhs);//用处不是很大,可以得到两个四元数旋转的夹角,然后除于2,得到夹角的中间值。

 

 

六、计算两个旋转的插值

从开始向量到目标向量的旋转

void start(){

  Quaternion rot = Quaternion.FromToRotation(new Vector3(0,0,1),new Vector3(1,0,1));//里面的两个参数是初始向量和结束向量,得到的差值是一个四元数

}

 

 

七.Vector3

1.坐标

2.向量:运算和数学里面是一样的。

  a.向量的加法

  (x1,y1,z1)+(x2,y2,z2)

 

  b.向量的减法

  (x1,y1,z1)-(x2,y2,z2)

 

  c.向量的乘除法:相当于缩放

    Vector3 vec = new Vector3(1, 0, 1);
    vec = vec * -3;
    Debug.Log(vec);

 

  d.向量的模:开根号,得到标量

 

  e.向量的等于和不等于:==和!=

  (x1,y1,z1)==(x2,y2,z2)

 

  f.向量平移

    void update(){

      float speed = 1;
      float s = speed * Time.deltaTime;
      this.cube.transform.position += (Vector3.forward * s);

    }

 

  g.单位向量

    Debug.Log(Vector3.zero);//(0,0,0)

    Debug.Log(Vector3.one);//(1,1,1)

    Debug.Log(Vector3.up);//(0,1,0)

    Debug.Log(Vector3.forward);//(0,0,1)

    Debug.Log(Vector3.right);//(1,0,0)

 

  e.向量的线性插值,可以实现一个位置到另外一个位置

    from...to...

    0是起点,1是终点,0.5是中心位置

    from+(to-from)*t,t的取值范围是(0,1)

    void update(){

      // from (-10, 0, 0), to(10, 0, 0);
      this.cube.transform.position = Vector3.Lerp(Vector3.zero, Vector3.right * 10, Time.time);//Time.time是每次累积的总时间,这里作为t,可以设置一下来调整移动的速度,Time.time*0.1f放慢

    }//从一个点平移到另外一个点

 

  f.向量的球形插值

    void update(){

       this.cube.transform.position = Vector3.Slerp(Vector3.right * -10, Vector3.right * 10, Time.time * 0.1f);

    }//从一个点转到到另外一个点

  

  g.两个向量之间的距离 

     // distance两个向量的距离
     float len = Vector3.Distance(Vector3.right, Vector3.right * -1);
     Debug.Log(len);

 

  h.两个向量之间的角度

     float degree = Vector3.Angle(Vector3.right, Vector3.right * -1);
     Debug.Log(degree);

 

3.欧拉角

 

八、获取组件实例

所以的组件实例都有一个成员变量名字为transform的,指向所在节点的transform组件实例,包括transform组件实例也有成员变量transform,因为它自己也是一个组件,gameObject也有成员变量transform指向自己的transform组件实例。

所有组件实例都有一个成员变量gameObject,指向所在节点。

 

用老师的话讲

// 凡是组件,都会有一个成员为gameObject可以获得这个组件实例所绑定的节点。
// 凡是组件,都会有一个成员为transrom 可以获得这个组件实例所绑定节点的transform组件。
// 凡是GameObject都会有一个成员 transfrom指向 它所挂载的Transrom组件的实例。

 

1.通过gameObject获得挂载的组件

  // 获得节点上所挂的第一个transfrom组件实例
  Transform tran = this.gameObject.GetComponent();


  // 通过GameObject, 返回这个节点所挂的所有为 这种类型的组件
  this.gameObject.GetComponents();
  this.cube.gameObject.GetComponent();
  this.cube.gameObject.GetComponent("cube");

2.通过组件,获得当前组件所挂的节点的其他组件
  this.GetComponent();
  this.cube.GetComponent();
  this.cube.GetComponent("cube");

 

九. MonoBehaviour将参数绑定到编辑器

只要把组件类的成员变量的权限设置成public,就可以在Unity编辑器界面显示出参数。

 

你可能感兴趣的:(关于Unity中的transform组件(三))