Camera Perspective
Updated
Normal 3D camera: far things look smaller.
- Type
ent-camera-perspective- Script class
PerspectiveCameraEntity- Family
- Cameras
Overview
Section titled “Overview”A Camera Perspective entity is a camera that sees like an eye: things further away look smaller. It is the camera for almost every scene: first-person and third-person views, fly-throughs, security cameras, photo booths.
Its lens is its required Camera Perspective component (comp-camera-perspective), and its transform places and aims it: you frame a shot by moving and rotating the entity. The camera looks along its local -Z axis. The projection component cannot be removed or switched off; to turn a camera off, switch off its entity.
Cameras render only in play. In the editor the viewport has its own camera, and each placed camera shows its frustum and an icon you click to select it. Which camera the player sees is decided by the Main Camera component, which marks the scene’s player view, and by CameraDirector, which lets scripts cut to other cameras.
Pick a perspective camera unless you want a view without depth. For top-down maps, isometric views and 2D-style games, use a Camera Orthographic.
The props:
fovis the vertical field of view, in degrees, from1to179,60by default. A smaller value zooms in; a larger one widens the view.nearis the near clip plane,0.1m by default. Anything closer is not drawn.faris the far clip plane,2000m by default. Anything farther is not drawn.
A camera entity also takes optional components: a Main Camera, one input controller (First Person, Third Person, Orbit or Fly), a Motion Path for fly-throughs, Camera Collision, camera and post-processing effects, and a Signal Emitter.
Set it up
Section titled “Set it up”- Add a Camera Perspective entity to the scene (see Scenes and hierarchy). A new camera gets a Main Camera when its scene has none.
- Move and rotate it to frame the view, or add a controller such as First Person or Third Person so the player drives it.
- Set
fov; keepnearas large andfaras small as the scene allows. - For extra cameras (cutscenes, security feeds), leave out the Main Camera and cut to them from a script with
CameraDirector.switchTo.
Examples
Section titled “Examples”Zoom a scope while the right button is held
Section titled “Zoom a scope while the right button is held”Goal: narrow the field of view smoothly while the right mouse button is held, and widen it again on release.
The script is on the player’s camera.
#pragma lifecycle(startup, update)
const WIDE = 60;const ZOOM = 20;const SPEED = 8;
let lens;let fov = WIDE;
function startup() { lens = self.parent.getComponent(PerspectiveCameraComponent);}
function update(dt) { const goal = Input.mouse.isButtonPressed(MouseButton.Right) ? ZOOM : WIDE; fov += (goal - fov) * Math.min(1, SPEED * dt); if (Math.abs(goal - fov) > 0.01) lens.setProps({ fov });}self.parent.getComponent(PerspectiveCameraComponent)returns the camera’s lens component.Input.mouse.isButtonPressed(MouseButton.Right)is true while the right button is down.- Each frame,
fovmoves part of the way toward its goal.Math.min(1, SPEED * dt)makes the easing the same at any frame rate. setPropsis called only while the value is still changing, so an idle lens costs nothing.
Play an intro cutscene
Section titled “Play an intro cutscene”Goal: at startup, cut to a cutscene camera that flies along a path, then return to the player’s view when the path ends.
Cutscene Cam is a Camera Perspective without a Main Camera. It has a Motion Path controller with repeat: 'once' and autoStart: false, and a Signal Emitter whose signal is cutscene.
#pragma lifecycle(startup)
function startup() { const path = findEntity('Cutscene Cam').getComponent(MotionPathControllerComponent); const player = CameraDirector.main?.getComponent(CharacterControllerComponent);
on('cutscene', (event) => { if (event.eventType !== EventType.MotionPathController.End) return; CameraDirector.switchToMain(); player?.setEnabled(true); });
player?.setEnabled(false); CameraDirector.switchTo('Cutscene Cam'); path.play();}CameraDirector.mainis the main camera entity; its Character Controller, if any, is disabled so the player stands still during the intro.CameraDirector.switchTo('Cutscene Cam')cuts to the camera by name. It throws when no such camera exists.path.play()starts the Motion Path. Withrepeat: 'once', it sendsMotionPathController::endthrough the Signal Emitter when it reaches its last point.CameraDirector.switchToMain()cuts back to the player’s view, and the player gets control again.
Take a photo through a second camera
Section titled “Take a photo through a second camera”Goal: when another script emits booth/shoot, render a square photo through a camera that is not on screen.
#pragma lifecycle(startup)
function startup() { const booth = findEntity('Booth Cam').getComponent(PerspectiveCameraComponent);
on('booth/shoot', async () => { const photo = await booth.capture({ width: 1024, height: 1024, type: 'image/jpeg', quality: 0.85 }); log(`Captured ${photo.size} bytes`); emit('booth/photo', { photo }); });}capture(options)renders the scene through that camera off screen and resolves to an imageBlob, even when the camera is not the one rendering.- The listener is
async, so it canawaitthe picture. - JPEG with a
qualitybelow1gives smaller files. The picture leaves out post-processing effects and editor helpers. emit('booth/photo', { photo })hands the photo to another script, for example to show or upload it.
- A scene has one main camera. A second Main Camera in the same scene waits, with a console warning, until the first goes away.
- The
cameraglobal is the camera object rendering now, not an entity. To change props, useCameraDirector.currentorCameraDirector.main, which return camera entities. - A camera cannot switch projection. Add a Camera Orthographic and cut to it instead.
Components
Section titled “Components”What a new Camera Perspective is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Camera Perspective | comp- |
Required | One | Normal 3D camera: far things look smaller. |
You can also add 37 optional components, such as Animation Graph, Animation Timeline, Ascii Effect, Bloom Effect, Camera Breathing, Camera Collision.
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
Your first camera script: press C to cut from the main camera to a security camera with CameraDirector.switchTo, and C again to come back.
Click the view, then press C to switch cameras, and C again to switch back.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Camera Perspective.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Camera Perspective'); if (!(entity instanceof PerspectiveCameraEntity)) return;
// Its Camera Perspective component holds the settings listed under Props. const component = entity.getComponent(PerspectiveCameraComponent); log(component.props);}