在Unity 2d中使用坐标系和游戏屏幕吗? [英] Working with the coordinate system and game screen in Unity 2d?

查看:327
本文介绍了在Unity 2d中使用坐标系和游戏屏幕吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在其他平台上开发了游戏,这些平台对我来说x / y坐标系很有意义。左上角代表坐标为(0,0)的游戏屏幕,右下角为(width,height)。现在,我试图跳到Unity 2d,但我不明白游戏屏幕的工作原理。如果我在屏幕上有一个背景对象和一个角色对象,则当我将角色移动到他的x和y值时,其值在-3和3之间变化...非常小的坐标,它与我设置的游戏分辨率不符( 1024x768)。有没有很好的教程来理解Unity中的游戏网格?还是有人可以解释我如何完成我想做的事情?

So I've developed games in other platforms where the x/y coordinate system made sense to me. The top left representing the game screen with coordinates of (0,0) and the bottom right was (width,height). Now I'm trying to make the jump to Unity 2d and I can't understand how the game screen works. If I had a background object and a character object on the screen, when I move the character around his x and y values vary between -3 and 3... very small coordinates and it doesn't match the game resolution I have setup (1024x768). Are there good tutorials for understanding the game grid in Unity? Or can anyone explain how I can accomplish what I'm trying to do?

推荐答案

Unity中有三个坐标系:屏幕坐标,查看坐标和世界坐标。

There are three coordinates systems in Unity: Screen coordinates, view coordinates and the world coordinates.

世界坐标:使用点考虑场景中对象的绝对位置。您可以选择使单位代表您想要的任何长度,例如1个单位= 10米。屏幕上实际显示的内容取决于相机的放置位置和方向。

World coordinates: Think of the absolute positioning of the objects in your scene, using "points". You can choose to have the units represent any length you want, for example 1 unit = 10 meters. What is actually shown on the screen is determined by where the camera is placed and how it is oriented.

查看坐标:给定相机的视口。视口是一个虚构的矩形,通过它可以查看世界。这些坐标是比例的,范围从(0,0)到(1,1)。

View Coordinates: The coordinates in the viewport of a given camera. Viewport is the imaginary rectangle through which the world is viewed. These coordinates are porportional, and range from (0,0) to (1,1).

屏幕坐标:实际像素坐标表示设备屏幕上的位置。

Screen Coordinates: The actual pixel coordinates denoting the position on the device's screen.

请注意,任何给定对象的世界坐标总是相同的,而不管使用哪个相机进行查看,而视图坐标取决于所使用的相机。屏幕坐标还取决于设备的分辨率和相机视图在屏幕上的位置。

Note that the world co-ordinates of any given object will always be the same regardless of which camera is used to view, whereas the view coordinates depends on the camera being used. The screen coordinates in addition depend on the resolution of the device and the placement of the camera view on the screen.

相机对象提供了几种在这些之间进行转换的方法。不同的坐标系,例如 ScreenToViewportPoint, ScreenToWorldPoint等。

The "Camera" object provides several methods to convert between these different coordinate systems like "ScreenToViewportPoint" "ScreenToWorldPoint" etc.

示例:将对象放在屏幕左上方

    float distanceFromCamera = 10.0f;
    Vector3 pos = Camera.main.ScreenToWorldPoint (new Vector3 (0, Screen.height, distanceFromCamera));

    transform.position = pos;

ScreenToWorldPoint 函数将Vector3作为参数,其中x和y表示屏幕上的像素位置(0,0在左下方),z分量表示距摄像机的期望距离。无限数量的3D位置可以映射到同一屏幕位置,因此您需要提供此值。

The ScreenToWorldPoint function takes a Vector3 as an argument, where the x and y denote the pixel position on the screen ( 0,0 is the bottom left) and the z component denotes the desired distance from the camera. An infinite number of 3D locations can map to the same screen position, so you need to provide this value.

只要确保所需位置在相机的剪切区域内即可。另外,您可能需要为对象选择合适的枢轴,具体取决于要在对象的哪一部分位于左上角。

Just make sure that the desired position falls within the clipping region of the camera. Also, you might need to pick a proper pivot for your object depending on which part of your object you want centered on the top left.

这篇关于在Unity 2d中使用坐标系和游戏屏幕吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆