Game Development Reference
In-Depth Information
Unity Specifics
Screen, Viewport, and World Vectors
Unity, as with all 3D graphics systems, has three coordinate systems: screen
space, viewport, and world. The screen and the viewport are 2D systems,
and the world is 3D. The screen coordinates by default range from (0,0) to
(screen width in pixels, screen height in pixels). The viewport coordinates
are proportional, going from (0,0) to (1,1), and the world coordinates are
infinite. Having said that, the coordinates ( 10, 50) in screen space and
( 0.5, 1) in the viewport are legitimate. They just don't appear in the visible
range of the camera, just like 3D coordinates outside the camera frustum.
Unity has a number of useful functions for converting all these coordinate
systems among each other. They can all be found in the script reference
by searching for “Camera.” To try them out, create a new Unity Project with
the script in Listing 4.5 . Attach the script to the camera, and the converted
coordinates will be displayed in the console.
Listing 4.5 Code to Convert the Mouse Position on the
Screen to Viewport and World Coordinates
function Update ()
print("Screen to Viewport" +
print("Screen to World" +
At first the coordinates may not change when you move the mouse
over the screen. This will occur if the camera is in perspective mode
and the near plane distance is very small—the reason being that the
closer the near plane is to the camera, the more the frustum becomes
a pyramid with the screen space representing the apex. Therefore, the
screen is almost a single point in 3D space and essentially you are moving
the mouse around on this point as far as the 3D coordinate system is
concerned. Changing the camera to orthographic will give a square view
volume and provide more screen space area with respect to the 3D world.
You can also move the near plane farther away from the camera if you
want the mouse position changes to register.
There are also functions called ViewportToScreenPoint(),
ViewportToWorldPoint(), WorldToScreenPoint(), and
Search Nedrilad ::

Custom Search