A Camera decides what the player sees. It also converts between screen positions (pixels, like where the mouse is) and world positions (where things are in the scene).
The camera is a window you look through at the world. The screen is the glass. ScreenPointToRay is putting your finger on the glass and asking "what am I pointing at out there?" WorldToScreenPoint is the reverse: "where on the glass do I see that tree?"
Screen and viewport coordinates both start at the bottom-left, in pixels or 0 to 1. ScreenPointToRay turns a point on the screen into a ray from the camera into the world; pass it to Physics.Raycast to find what's under the mouse.
Static Properties
Name
Type
Description
Camera.main
Camera
The first enabled camera tagged "MainCamera". null if none is tagged.
Properties
Name
Type
Description
fieldOfView
float
Vertical view angle in degrees (perspective). Lower = zoomed in.
orthographic
bool
True for flat 2D-style views with no perspective.
orthographicSize
float
Half the height of the view in world units (orthographic). Lower = zoomed in.
nearClipPlane / farClipPlane
float
Things closer/further than these aren't drawn.
backgroundColor
Color
Fill color when Clear Flags / Background Type is Solid Color.
cullingMask
int
Which layers this camera draws.
depth
float
Draw order when there are several cameras (Built-in pipeline). Higher draws on top.
Methods
Method
Returns
Description
ScreenPointToRay(Vector3 pos)
Ray
A ray from the camera through a screen pixel. Use with Physics.Raycast for mouse picking.
ViewportPointToRay(Vector3 pos)
Ray
Same with viewport coordinates. (0.5, 0.5) = crosshair.
ScreenToWorldPoint(Vector3 pos)
Vector3
Pixel → world. pos.z must be the distance from the camera.
WorldToScreenPoint(Vector3 pos)
Vector3
World → pixel. z is the distance; negative means behind the camera.
WorldToViewportPoint(Vector3 pos)
Vector3
World → 0-1 viewport. Handy for "is this on screen?"
Code Samples
2D: move an object to the mouse
using UnityEngine;
public class FollowMouse2D : MonoBehaviour
{
private Camera cam;
void Awake() { cam = Camera.main; } // cache it
void Update()
{
Vector3 mouse = Input.mousePosition;
mouse.z = -cam.transform.position.z; // distance from camera to the z = 0 plane
Vector3 world = cam.ScreenToWorldPoint(mouse);
transform.position = new Vector3(world.x, world.y, 0f);
}
}
After LateUpdate, every enabled camera renders what it can see on its cullingMask layers, between its near and far clip planes.
That's why camera movement belongs in LateUpdate: everything has moved for this frame, and the camera renders right after.
Camera.main is cached by Unity in recent versions, so it's cheap, but it's still tidy to store it in a field.
Common Mistakes
Camera.main is null. Your camera isn't tagged "MainCamera".
ScreenToWorldPoint always returns the camera position. You left z = 0. For a perspective camera, z must be how far in front of the camera you want the point.
Jittery follow camera. Move it in LateUpdate, and if following a Rigidbody, turn on Interpolate on that Rigidbody.
For polished cameras, consider the Cinemachine package: follow, look-at, screen shake and blending between shots without code.