Camera
TypeClass (Component)
NamespaceUnityEngine
InheritsBehaviour > Component > Object

Camera

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?"

Coordinate systems

SpaceUnitsExample
ScreenPixels. (0,0) bottom-left, (Screen.width, Screen.height) top-rightInput.mousePosition
Viewport0 to 1. (0.5, 0.5) is the center"Center of the screen" on any resolution
WorldScene units (meters)transform.position
The screen(0, 0) pixels(0, 0) viewport(width, height) px(1, 1) viewportInput.mousePositionThe scene, seen from the sideCameraScreenPointToRay(…)under the mouse
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

NameTypeDescription
Camera.mainCameraThe first enabled camera tagged "MainCamera". null if none is tagged.

Properties

NameTypeDescription
fieldOfViewfloatVertical view angle in degrees (perspective). Lower = zoomed in.
orthographicboolTrue for flat 2D-style views with no perspective.
orthographicSizefloatHalf the height of the view in world units (orthographic). Lower = zoomed in.
nearClipPlane / farClipPlanefloatThings closer/further than these aren't drawn.
backgroundColorColorFill color when Clear Flags / Background Type is Solid Color.
cullingMaskintWhich layers this camera draws.
depthfloatDraw order when there are several cameras (Built-in pipeline). Higher draws on top.

Methods

MethodReturnsDescription
ScreenPointToRay(Vector3 pos)RayA ray from the camera through a screen pixel. Use with Physics.Raycast for mouse picking.
ViewportPointToRay(Vector3 pos)RaySame with viewport coordinates. (0.5, 0.5) = crosshair.
ScreenToWorldPoint(Vector3 pos)Vector3Pixel → world. pos.z must be the distance from the camera.
WorldToScreenPoint(Vector3 pos)Vector3World → pixel. z is the distance; negative means behind the camera.
WorldToViewportPoint(Vector3 pos)Vector3World → 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);
    }
}

Health bar over an enemy's head (Screen Space UI)

[SerializeField] private Transform enemyHead;
[SerializeField] private RectTransform healthBarUI;

void LateUpdate()
{
    Vector3 screen = Camera.main.WorldToScreenPoint(enemyHead.position);
    bool inFront = screen.z > 0f;
    healthBarUI.gameObject.SetActive(inFront);
    if (inFront) healthBarUI.position = screen;
}

Is an object on screen?

Vector3 v = cam.WorldToViewportPoint(target.position);
bool onScreen = v.z > 0f && v.x >= 0f && v.x <= 1f && v.y >= 0f && v.y <= 1f;

What happens at runtime

  1. After LateUpdate, every enabled camera renders what it can see on its cullingMask layers, between its near and far clip planes.
  2. That's why camera movement belongs in LateUpdate: everything has moved for this frame, and the camera renders right after.
  3. 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

See also