Understanding Unity 3D coordinate systems
Unity 3D has 4 coordinate systems: World, Screen, Viewport and UI.
The world coordinate system is left handed (as Direct X) where x positive axis is right, y positive is up and z is positive into the screen.
Screen coordinate system is bottom-up: (0,0) at bottom-left corner and (pixelWidth-1,pixelHeight-1) at right-top; x axis is positive right and y is positive up. The z position is in world units from the camera.
Viewport coordinate system is normalized and relative to the camera, so the bottom-left point is (0,0), the top-right is (1,1). The z position is in world units from the camera.
Finally UI coordinate system is top-down: the y coordinate varies from zero at the top edge of the window to a maximum at the bottom edge of the window. The upper-left point is (0,0); the bottom-right is (1,1)
There are many functions to convert between all these different coordinate systems, see
Camera.WorldToScreenPoint
Camera.WorldToViewportPoint
Camera.ScreenToViewportPoint
Camera.ScreenToWorldPoint
Camera.ViewportToScreenPoint
Camera.ViewportToWorldPoint
GUIUtility.ScreenToGUIPoint
GUIUtility.GUIToScreenPoint
Understanding Unity 3D coordinate systems
There are four coordinate system in Unity: Screen, World, Viewport, and GUI.
Screen coordinates are 2D, measured in pixels and start in the lower left corner at (0,0) and go to (Screen.width, Screen.height). Screen coordinates change with the resolution of the device, and even the orientation (if you app allows it) on mobile devices.
GUI coordinates are used by the GUI system. They are identical to Screen coordinates except that they start at (0,0) in the upper left and go to (Screen.width, Screen.height) in the lower right.
Viewport coordinates are the same no matter what the resolution. The are 2D, start at (0,0) in the lower left and go to (1,1) in the upper right. For example (0.5, 0.5) in viewport coordinates will be the center of the screen no matter what resolution or orientation.
World coordinates are a 3D coordinates system and where all of your object live.
So here you appear to be asking about converting Screen coordinates to World coordinates. What you can do to make that conversion will vary a bit between a Perspective camera and an Orthographic camera.
So to visualize a Perspective camera, imagine you are standing at a window, and you reach out and a bit to your right and touch the glass. Imagine you then sight down your finger looking at objects outside. The sighting might find a near object like a tree, or a far object like a cloud or a mountain. Though the finger does not move, the distance from the finger out into the world makes a big difference in the world coordinate of that object.
So to convert a Screen coordinate to a world coordinate, you need to specify the distance from the camera plane to the plane in the world...that is, a distance in front of the camera, as a 'z' parameter. The code might look like:
Vector3 pos = Input.mousePosition;
pos.z = 10;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
This code finds the world position of the mouse ten units in front of the camera.
'Projects > Unity' 카테고리의 다른 글
Basic Concepts (1) | 2019.01.20 |
---|