Unity学习记录-Transform缩放、看向和坐标系转换

缩放

  • 获取相对于世界坐标系的缩放

    1
    print(this.transform.lossyScale);
  • 获取相对于本地坐标系(即父对象)的缩放

    1
    print(this.transform.localScale);
  • Unity没有提供缩放的API,缩放要自己实现

    1
    this.transform.localScale += Vector3.one*Time.deltaTime;

看向

看向即让物体的Z轴朝向某个方向,仅仅适用于3D

  • 让物体看向一个点(相对于世界坐标系)

    1
    this.transform.LookAt(Vector3.zero)
  • 让物体看向一个对象

    1
    this.transform.LookAt(sun.transform)

坐标系转换

世界坐标系的点转本地坐标系

  • 受到缩放影响

    1
    this.transform.InverseTransformPoint(Vector3.one);

世界坐标系的方向转本地坐标系

  • 受到缩放影响(Vector)

    1
    this.transform.InverseTransformVector(Vector3.forward);
  • 不受缩放影响(Direction)

    1
    this.transform.InverseTransformDirection(Vector3.forward);

本地坐标系转世界坐标系

在创建技能特效时可以将本地坐标系转换为世界坐标系

  • 本地的点转换为世界坐标系的点

    1
    this.transform.TransformPoint(Vector3.zero);
  • 本地的方向转换为世界坐标系的方向

    • 受缩放影响

      1
      this.transform.TransformVector(Vector3.forward);
    • 不受缩放影响

      1
      this.transform.TransformDirection(Vector3.forward);