Skip to main content
Every scene requires at least one CameraComponent to render. Add it to a GameObject — the component uses the object’s world position and rotation to determine the camera’s view. Multiple cameras can coexist; IsMainCamera and Priority control which one the player sees.
CameraComponent is sealed. Extend camera behaviour by writing a separate component that reads or writes properties on the camera each frame.

Projection properties

FieldOfView
float
default:"60"
Horizontal (or vertical, depending on FovAxis) field of view in degrees. Valid range is 1–179. Hidden when Orthographic is true.
FovAxis
Axis
default:"Horizontal"
The axis along which FieldOfView is measured. Horizontal fits the view within the x-axis; Vertical fits it within the y-axis.
Orthographic
bool
default:"false"
When true, the camera uses orthographic projection instead of perspective. Useful for 2D games or UI previews.
OrthographicHeight
float
default:"1204"
The height of the orthographic view in world units. Only applies when Orthographic is true.
ZNear
float
default:"10"
Near clip plane distance. Objects closer than this are not rendered. Keep this above 1 to avoid depth-buffer artefacts such as z-fighting.
ZFar
float
default:"10000"
Far clip plane distance. Objects beyond this are not rendered. Balance against ZNear — a larger ZNear lets you push ZFar further without precision loss.

Camera selection

IsMainCamera
bool
default:"true"
Marks this camera as the main game camera. The scene uses the active camera with the highest Priority among all cameras that have IsMainCamera set to true.
Priority
int
default:"1"
Determines render order when multiple cameras are active. Higher value renders on top. Range 1–16.

Rendering

BackgroundColor
Color
default:"#557685"
The background color shown when no sky is present in the scene. Only used when ClearFlags includes Color.
ClearFlags
ClearFlags
default:"Color | Stencil | Depth"
Controls which buffers are cleared before rendering. The default clears color, stencil, and depth.
RenderTags
TagSet
Only GameObjects whose tags match any tag in this set are rendered. If the set is empty, all objects are rendered.
RenderExcludeTags
TagSet
GameObjects whose tags match any tag in this set are excluded from rendering. Applied after RenderTags.
Viewport
Vector4
default:"0, 0, 1, 1"
The portion of the screen this camera renders to, normalized 0–1. The components are (x, y, width, height).
RenderTexture
RenderTextureAsset
When set, the camera renders to this texture asset instead of the screen. Useful for security cameras, mirrors, and preview windows.

Post-processing

EnablePostProcessing
bool
default:"true"
Enables or disables all post-processing effects for this camera. Disable to get a raw, unprocessed render.
PostProcessAnchor
GameObject
If set, post-process volume lookups use this GameObject’s position rather than the camera’s position. Useful when the camera is far from the scene action.

Utility methods

MethodDescription
PointToScreenNormal(Vector3)Converts a world-space position to normalized screen coordinates (0–1).
PointToScreenPixels(Vector3)Converts a world-space position to screen pixel coordinates.
ScreenPixelToRay(Vector2)Returns a ray from the camera through a screen pixel. Useful for mouse picking.
ScreenNormalToRay(Vector3)Returns a ray from the camera through a normalized screen position.
ScreenToWorld(Vector2)Converts screen coordinates to a world-space point on the near frustum plane.
GetFrustum()Returns the view frustum for the current screen rect.
RenderToTexture(Texture)Renders the scene from this camera’s view into the supplied render target texture.
UpdateSceneCamera(SceneCamera)Manually pushes all component settings to a SceneCamera object.

Code examples

Setting up a camera from code

using Sandbox;

public sealed class CameraSetup : Component
{
    protected override void OnStart()
    {
        var cam = Components.GetOrCreate<CameraComponent>();

        cam.FieldOfView   = 90f;
        cam.ZNear         = 5f;
        cam.ZFar          = 20000f;
        cam.BackgroundColor = Color.Black;
        cam.IsMainCamera  = true;

        // Exclude objects tagged "ui-world" from being seen by this camera
        cam.RenderExcludeTags.Add( "ui-world" );
    }
}

Switching the main camera

Use IsMainCamera to hand rendering off between cameras. The engine automatically picks the highest-priority active main camera.
using Sandbox;

public sealed class CameraSwitcher : Component
{
    [Property] public CameraComponent FirstPersonCamera { get; set; }
    [Property] public CameraComponent ThirdPersonCamera { get; set; }

    bool _isFirstPerson = true;

    protected override void OnUpdate()
    {
        if ( Input.Pressed( "camera_toggle" ) )
        {
            _isFirstPerson = !_isFirstPerson;

            FirstPersonCamera.IsMainCamera  = _isFirstPerson;
            ThirdPersonCamera.IsMainCamera  = !_isFirstPerson;
        }
    }
}
Give each camera a unique Priority if you ever enable both at the same time. The highest priority camera renders last and appears on top.

Build docs developers (and LLMs) love