Four coordinate systems of Unity3D

[ Four coordinate systems of Unity3D ]

1. World Space (world coordinates): We add objects (such as: Cube) to the scene, and they are displayed in the scene in world coordinates. transform.position can get the position coordinates.

2. Screen Space (screen coordinates): defined in pixels, the lower left corner of the screen is the (0, 0) point, the upper right corner is (Screen.width, Screen.height) , the position of Z is the world unit of the camera to measure. Note: The mouse position coordinates belong to the screen coordinates, Input.mousePosition can obtain the position coordinates, and the finger touches the screen is also the screen coordinates, Input.GetTouch(0).position can obtain the screen coordinates of a single finger touch.

  Screen.width = Camera.pixelWidth

  Screen.height = Camera.pixelHeigth

3. ViewPort Space: Viewport coordinates are standard and relative to the camera. The lower left corner of the camera is the (0, 0) point, the upper right corner is the (1, 1) point, and the Z position is measured in the camera's world units.

4. The coordinate system for drawing the GUI interface: This coordinate system is similar to the screen coordinate system, except that the coordinate system takes the upper left corner of the screen as the (0, 0) point, and the lower right corner as (Screen.width, Screen.height) .

[ Conversion of four coordinate systems ]

1. World coordinates → screen coordinates: camera.WorldToScreenPoint(transform.position); This can convert world coordinates to screen coordinates. Where camera is the camera object in the scene.

2. Screen coordinates→Viewport coordinates: camera.ScreenToViewportPoint(Input.GetTouch(0).position); This can convert screen coordinates to viewport coordinates. Where camera is the camera object in the scene.

3. Viewport coordinates → screen coordinates: camera.ViewportToScreenPoint();

4. Viewport coordinates → world coordinates: camera.ViewportToWorldPoint();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326964122&siteId=291194637