Unity——Transform类

transform.position

基础知识:

Transform作为Unity中游戏对象最为重要的组件,“位置”作为Transfrom组件的一个属性,具有同等重要的地位。

Transform组件下有两个“位置”属性:

position:指的是游戏对象中心点在世界空间的位置(世界空间就是Unity最大的空间,可以创建一个无任何父节点的空对象,其position,rotation分量都是0,scale分量都是1,那么可以认为世界空间就是以此物体中心点为原点,物体的朝向为z轴,右方向为x轴,上方向为y轴,即物体的transform.forward为z轴,transform.right为x轴,transform.up为y轴,如图中的SuperParent物体)。

SuperParent1.jpg

localPosition:指的是游戏对象中心点在父类空间的位置(属性面板上显示的值就是这个值)。

父类空间的构建与其position,rotation,scale有关,但此处只讨论position,因此可以认为父类物体不旋转,没有缩放的情况。那么此时父类空间是以父类中心点为原点,x,y,z与世界空间x,y,z轴同方向。

如果游戏对象没有父类,那么此物体的父类空间就是世界空间,此时position和localPosition值是相同的。可以想象为,其父类就是文章上面的SuperParent物体。

UGUI下所有的UI空间都具有RectTransform组件,此组件继承自Transform组件,其内部的position和localPosition属性也继承自Transform。他多出来的位置属性是:

anchoredPosition3D:指的是物体中心点Pivot相对于锚点Anchor的位置(UI属性面板上显示的就是这个值,即PosX,PosY,PosZ)。

注意:

1、 这里指的是锚点,尽管锚点是附加在父类上的,但其并不是父类空间,父类空间指的是以其中心点为原点,而此处指的是以锚点为原点。

2、 RectTransform提供了我们足够的自由去更改UI的锚点位置和中心点的位置。但是万变不离其宗:anchoredPosition3D核心概念没变:中心点相对锚点的位置。

3、 anchoredPosition是anchoredPosition3D属性去除了z轴之后的值。

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的区别
 

发布了97 篇原创文章 · 获赞 21 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/sun124608666/article/details/100708225
今日推荐