Transform类

来源:互联网 发布:oracle 数据库迁移 编辑:程序博客网 时间:2024/05/17 23:51

Rotate 绕某一向量旋转

public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);
public class ExampleClass : MonoBehaviour {    void Update() {        transform.Rotate(Vector3.right * Time.deltaTime);        transform.Rotate(Vector3.up * Time.deltaTime, Space.World);    }}

public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
public class ExampleClass : MonoBehaviour {    void Update() {        transform.Rotate(Time.deltaTime, 0, 0);        transform.Rotate(0, Time.deltaTime, 0, Space.World);    }}

public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);
public class ExampleClass : MonoBehaviour {    void Update() {        transform.Rotate(Vector3.right, Time.deltaTime);        transform.Rotate(Vector3.up, Time.deltaTime, Space.World);    }}
其中第二个参数 Space.World表示绕世界坐标轴旋转,Space.Self表示绕自身坐标轴旋转
我们来看一下第二个参数的效果
创建一个Cube,随意旋转一下Cube,让Cube的坐标轴与世界坐标轴不同


创建脚本t_Rotate.cs
首先看一下Space.Self效果
代码
public class t_Rotate : MonoBehaviour {    void Update()    {        transform.Rotate(Vector3.up * Time.deltaTime * 20, Space.Self);    }}

效果


我们看到是围绕自身的Y坐标轴旋转的
下面看一下Space.World的效果
代码
public class t_Rotate : MonoBehaviour {    void Update()    {        transform.Rotate(Vector3.up * Time.deltaTime * 20, Space.World);    }}
效果


我们看到是围绕世界坐标系中的Y轴旋转的。

RotateAround 绕某一点的某个轴旋转

public void RotateAround(Vector3 point, Vector3 axis, float angle);
功能:绕point点的axis轴旋转
例子: 
假如我们想使Cube绕Sphere物体的Vector3(0,1,1)轴旋转
代码:
public class t_RotateAround : MonoBehaviour {    public Transform target;void Update () {        transform.RotateAround(target.position, new Vector3(0, 1, 1), 20 * Time.deltaTime);    }}
效果:



LookAt 朝向某一物体或者是世界中的某一点

public void LookAt(Transform target, Vector3 worldUp = Vector3.up);
public void LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up);


例子:我们在RotateAround例子中加入一句话使Cube的前方始终朝向Sphere
代码:
public class t_RotateAround : MonoBehaviour {    public Transform target;void Update () {        transform.RotateAround(target.position, new Vector3(0, 1, 1), 20 * Time.deltaTime);        //使Cube的前方(在这里是Z轴)始终朝向Sphere        transform.LookAt(target);    }}

效果



TransformDirection 将向量从局部坐标系转换到世界坐标系

public Vector3 TransformDirection(Vector3 direction);
该转换不受物体缩放和位置的影响


TransformPoint 将点从局部坐标系转换到世界坐标系

public Vector3 TransformPoint(Vector3 position);
返回点在世界坐标系下的位置,受到物体缩放的影响

InverseTransformPoint 将点从世界坐标系转换到局部坐标系

public Vector3 InverseTransformPoint(Vector3 position);
返回点在局部坐标系下的位置,受到物体缩放的影响

Translate

public void Translate(Vector3 translation, Space relativeTo = Space.Self);
public void Translate(float x, float y, float z, Space relativeTo = Space.Self);
相对坐标系移动
其中第二个参数 Space.World表示相对世界坐标系移动,Space.Self表示相对自身坐标系移动

public void Translate(Vector3 translation, Transform relativeTo);
public void Translate(float x, float y, float z, Transform relativeTo);
相对物体relativeTo的坐标系移动


lossyScale 和 LocalScale

localscale 本地坐标系的大小,也就是检视面板显示的数值
lossyscale 世界坐标系的大小,没有父物体时检视面板显示的数值,也就是相对于世界的实际大小
这两个的区别相当于localPosition和position的区别







































0 0
原创粉丝点击