Game Development Reference
In-Depth Information
Screen space and world space
When dealing with either touch or mouse input, we have to recognize that coordinates
managed by Unity are in screen space and not world space . Both of these terms are
defined as follows:
Screen space : This refers to coordinates relative to the screen / display area, start-
ing at the top-left corner of the screen
World space : This refers to the coordinates that are used inside the Unity engine
Tip
The starting position, (0, 0) 2D coordinate, for the mouse can be different depending on
how you access it:
• If you use Input.mousePosition , then (0, 0) is in the top-left corner of the
screen
• If, however, you use Event.current.mousePosition , then (0, 0) is in the
bottom-left corner of the screen
For touch, however, it is always in the the top-left corner of the screen. This is something
to keep in mind when you access input—always check.
When you poll for the mouse position (which can only be one mouse), use the following
line of code:
Input.mousePosition
When we are looking at touch points (which can be many), we use the following line of
code:
Input.GetTouch(<touch index>).position
However, because the position we get back is always in screen space, we need to convert
it to world space when checking it against objects in the game world. Thankfully, this is
very simple to do using the following built-in Unity function:
Camera.main.ScreenToWorldPoint(<screenCoordinate>)
Search WWH ::




Custom Search