A Camera is a device through which the player views the world. Understanding Screen Point, World Point and Viewport point in Unity3D forms the basics of how camera can be used while determining the position of an object or mouse pointer.
Screen point is defined in pixels. The bottom-left of the screen is (0,0); bottom-right of the screen is (pixelWidth,0), left-top is (0, pixelHeight) and the right-top is (pixelWidth,pixelHeight). Let’s understand this with an image.
So, if the screen resolution is 1024×768,
The bottom-left of the screen is (0,0); bottom-right of the screen is (1024,0), top-left is (0, 768) and the top-right is (1024,768). The centre point will be (512, 384)
This point can be used to find which half of the screen is touched. If
1.mousePosition < pixelWidth/2, the touch is on the left side of the screen
2. mousePosition >= pixelWidth/2, the touch is on the right side of the screen
Another usage of this point may be to move an object to the point where the screen is clicked. It is illustrated in the coming sections.
A viewport space point is normalized and relative to the Camera. The points on the screen range between 0 and 1. You can determine the the x-coordinate by dividing the screenPoint.x by pixelWidth and the y-coordinate by dividing the screenPoint.y by pixelHeight. Accordingly, the bottom-left of the Camera is (0,0); bottom-right is (1,0) the top-right is (1,1); top-left is (0, 1). The centre point will be (0.5, 0.5).
An important point to note is that the viewport point is relative to the camera and an object placed according to the view port point stays at the same place on the screen always.
World Space Point
A world space point is defined in global coordinates (for example, Transform.position). It is the position of the object in world or global space.
Unity in-built functions
Unity offers in-built functions to inter-convert world point, viewport point and space points into each other.
Transforms position from screen space into world space.
Transforms position from screen space into viewport space.
Transforms position from viewport space into screen space.
Transforms position from viewport space into world space
Transforms position from world space into screen space.
Transforms position from world space into viewport space.
Let’s Learn Better!
Let us see an example on how to move from world to screen point and vice versa.
If there is an object lying in the world space and you want to move it to the position where you click (not changing the z position of the object), you can use the following set of code:
Vector3 myScreenPos = Camera.main.WorldToScreenPoint(transform.position); transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, myScreenPos.z));
myScreenPos gives you the position of the object in the screen Space. This point will help you keep the object in-tact in z-axis.
Input.mousePosition gives you the position of the mouse Pointer in x and y axis.
The new position of the object is fetched by converting the screen position of mousePointer in x and y axis and the screen position of object in z-axis.
You can use the similar concept for other conversions too.
I hope you would now have a better understanding of Screen Point, World Point and Viewport point.