Skip to content

Camera Perspective

Updated

Normal 3D camera: far things look smaller.

Type
ent-camera-perspective
Script class
PerspectiveCameraEntity
Family
Cameras

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:

  • fov is the vertical field of view, in degrees, from 1 to 179, 60 by default. A smaller value zooms in; a larger one widens the view.
  • near is the near clip plane, 0.1 m by default. Anything closer is not drawn.
  • far is the far clip plane, 2000 m 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.

  1. Add a Camera Perspective entity to the scene (see Scenes and hierarchy). A new camera gets a Main Camera when its scene has none.
  2. Move and rotate it to frame the view, or add a controller such as First Person or Third Person so the player drives it.
  3. Set fov; keep near as large and far as small as the scene allows.
  4. For extra cameras (cutscenes, security feeds), leave out the Main Camera and cut to them from a script with CameraDirector.switchTo.

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.

ScopeZoom.jx
#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, fov moves part of the way toward its goal. Math.min(1, SPEED * dt) makes the easing the same at any frame rate.
  • setProps is called only while the value is still changing, so an idle lens costs nothing.

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.

IntroCutscene.jx
#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.main is 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. With repeat: 'once', it sends MotionPathController::end through the Signal Emitter when it reaches its last point.
  • CameraDirector.switchToMain() cuts back to the player’s view, and the player gets control again.

Goal: when another script emits booth/shoot, render a square photo through a camera that is not on screen.

PhotoBooth.jx
#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 image Blob, even when the camera is not the one rendering.
  • The listener is async, so it can await the picture.
  • JPEG with a quality below 1 gives 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 camera global is the camera object rendering now, not an entity. To change props, use CameraDirector.current or CameraDirector.main, which return camera entities.
  • A camera cannot switch projection. Add a Camera Orthographic and cut to it instead.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Camera Perspective comp-camera-perspective 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.

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.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

PerspectiveCameraEntity.jx
#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);
}