一 、世界坐标系--全局坐标系:
用来描述游戏场景内所有物体位置和方向的基准,也称为世界坐标系。
Unity场景中创建的物体都是以全局坐标系中的坐标原点(0,0,0)来确定各自的位置

二、局部坐标系(物体坐标系,模型坐标系)
每个物体都有其独立的物体坐标系,并且随物体进行相同的移动或者旋转。
局部坐标系:是相对于父物体来说也就是当两个游戏对象互为父子关系,那么子物体会以父物体的坐标点为自身的坐标原点。

下面我们来看看我们的局部坐标和全局坐标的不同吧。首先我们将脚本挂载在子物体上,然后在脚本上写上我们的代码输出坐标。



我们再看看其打印结果看是否正确

三、相机坐标系:
根据观察位置和方向建立坐标系。(UGUI会详细讲述)
使用次此坐标可以方便判断物体是否在相机前方,以及物体间的先后遮挡顺序,它会优先渲染离他最近的物体。
它会优先渲染离他最近的物体。

四、屏幕坐标系:Screen Space
建立在屏幕上的二位坐标系,用来描述像素在屏幕上的位置。(像素在补充课件里面)以像素定义,以屏幕左下角为坐标原点(0,0),
右上角为(Screen.width,Screen.height),Z的位置是以相机的世界坐标位置来衡量的。
在我们手机、电脑都是有屏幕的,它的左下角就是屏幕坐标的原点。当然我们的屏幕坐标是二维的只有X,Y轴



世界坐标→屏幕坐标
camera.WorldToScreenPoint(obj.transform.position)
下面我们来看看如何使用吧,首先我们给摄像机挂载一个脚本,然后开始写我们的代码。


其他坐标系之间的转换
坐标系的转换:
1、局部坐标系转换到全局坐标系:
Transform.TransformPoint()
首先我们创建一个脚本让其挂载在我们的子物体上。


全局坐标系转换到局部坐标系:
Transform.InVerseTransformPoint()
首先我们创建一个脚本让其挂载在我们的子物体上。

打印结果:

向量如何转为局部坐标系呢?
Transform.TransformDirection

向量如何转为全部坐标系呢?
Transform. InVerseTransformDirection

五、View port Space 视口坐标
视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,
Z的位置是以相机的世界单位来衡量的。
屏幕坐标→视口坐标
camera.ScreenToViewportPoint(Input.GetTouch(0).position)
世界坐标→视口坐标
camera.WorldToViewportPoint(obj.transform.position));
六、GUI界面坐标系
这个坐标系与屏幕坐标系相似,不同的是该坐标系以屏幕的左上角为(0,0)点,右下角为(Screen.width,Screen.height)。
print("世界坐标" + obj.transform.position);
print("屏幕坐标" + Input.GetTouch(0).position);
网友评论