Skip to content

Camera director

Updated

A scene can hold many cameras, but only one renders at a time. The camera director chooses it. By default it renders the main camera; a script can cut to any other camera (a cutscene camera, a security monitor, a finish-line replay) and cut back. This page covers the CameraDirector global, how cuts behave, how to build a cutscene, and the Camera::relocate event the runtime sends whenever a camera is moved from outside.

  • Read Cameras to understand camera entities and the main camera.
  • Component events reach scripts through a Signal Emitter. See Events and signals.
  • Cutscenes below use the Motion Path controller from Camera controllers.

Every frame, the rendering camera is, in order:

  1. the camera a script cut to with CameraDirector.switchTo, while it still exists and is enabled;
  2. otherwise the base scene’s main camera;
  3. otherwise the first registered camera;
  4. otherwise the default canvas camera.

A cut lasts until a script calls switchToMain(), or until the cut-to camera goes away (it is disabled, removed, or its scene unloads). Then the view returns to the main camera.

Member What it does
switchTo(nameOrId) Cut to a camera entity, by name or entity id, found in the active scenes. Throws when there is no such camera.
switchToMain() Cut back to the main camera.
current The camera entity rendering now, or undefined for the default camera. Read-only.
main The main camera entity of the base scene, or undefined when it has none. Read-only.
  • The main camera’s controllers pause. A controller only acts while its camera renders, so First Person, Third Person, Orbit and Fly stop reading input while you are cut away.
  • Player input stays live. A Character Controller still walks while the view is elsewhere, and its movement is relative to the rendering camera. Disable the player’s Character Controller during a cutscene if the player should stand still.
  • Coming back is a clean cut. Third Person and Orbit frame their target where it is now, not where it was when you cut away.
  • Spawn and spot points still move the main camera, even while you are cut to another one.
  • The look changes with the camera. Each camera has its own post-processing effects, so the image follows the rendering camera from the same frame.

A Motion Path controller moves its entity along a curve at constant speed. On a camera it makes a fly-through. With repeat: 'once', it sends MotionPathController::end through the entity’s Signal Emitter when it reaches its last point: the moment to cut back.

A typical setup:

  • a Cutscene Cam (Perspective Camera, no Main Camera);
  • a Motion Path on it with its points, repeat: 'once' and autoStart: false;
  • a Signal Emitter on it with the signal cutscene/done and the event filter MotionPathController::end.

See the first example below for the script.

Spawn points, spot points, travel points and teleports move a camera with one shared rule:

  • When the camera’s controller follows a target (a character), the target is placed on the point and the camera comes along, keeping its offset.
  • Otherwise the camera stands on the point, its eyes at eye height above it. With an enabled Character Controller on the camera, the eye height is where its capsule rests (height / 2 - center.y); without one it is 1.6 m.

Then the runtime sends the system event Camera::relocate with this data:

Field What it is
camera The camera entity that moved.
yaw Its new facing in radians, when the move set one.

The camera’s controller takes over from the new pose:

Controller On Camera::relocate
First Person Takes the new facing and drops its look smoothing.
Third Person Takes the new facing and frames its target again.
Orbit Carries on from the new pose. A point target moves with the camera, distance ahead.
Fly Flies on from the new pose and facing.

Camera::relocate is a system event: scripts can listen to it with on, but cannot emit it.

1. Play an intro cutscene, then hand control to the player

Section titled “1. Play an intro cutscene, then hand control to the player”
IntroCutscene.jx
#pragma lifecycle(startup)
let path;
let player;
function startup() {
path = findEntity('Cutscene Cam').getComponent(MotionPathControllerComponent);
player = CameraDirector.main?.getComponent(CharacterControllerComponent);
once('cutscene/done', () => {
CameraDirector.switchToMain();
player?.setEnabled(true);
});
player?.setEnabled(false); // the player stands still during the intro
CameraDirector.switchTo('Cutscene Cam');
path.play();
}

At startup the script disables the player’s Character Controller, cuts to the cutscene camera and starts its path. The Signal Emitter’s filter lets only MotionPathController::end through, so the cutscene/done signal means “the path is finished”. once hears it a single time, cuts back to the main camera and gives the player control again.

2. Cycle through security cameras with a key

Section titled “2. Cycle through security cameras with a key”
SecurityMonitor.jx
#pragma lifecycle(update)
const FEEDS = ['Lobby Cam', 'Vault Cam', 'Roof Cam'];
let index = -1; // -1 means the player's own view
function update() {
if (Input.keyboard.isKeyReleased(Key.Tab)) {
index = index + 1 >= FEEDS.length ? -1 : index + 1;
if (index === -1) CameraDirector.switchToMain();
else CameraDirector.switchTo(FEEDS[index]);
log('Now viewing', CameraDirector.current?.name ?? 'default camera');
}
if (Input.keyboard.isKeyReleased(Key.Escape) && index !== -1) {
index = -1;
CameraDirector.switchToMain();
}
}

Each release of Tab moves to the next feed, wrapping back to the player’s own view after the last one. isKeyReleased is true for exactly one frame per press, so one press is one step. Escape returns to the main camera at any time. If a feed name does not exist, switchTo throws, so keep the list in sync with the scene.

ViewpointTour.jx
#pragma lifecycle(startup)
const STOPS = ['View North', 'View Harbour', 'View Tower'];
const SECONDS = 4;
function show(index) {
if (index >= STOPS.length) {
CameraDirector.switchToMain();
return;
}
CameraDirector.switchTo(STOPS[index]);
delay(show, SECONDS * 1000, index + 1);
}
function startup() {
show(0);
}

show cuts to one viewpoint, then schedules itself for the next one with delay(fn, ms, ...args), which calls show(index + 1) four seconds later. After the last stop it cuts back to the main camera. delay counts game time, so the tour pauses with the game, and it is cancelled automatically if the script is released.

ArrivalFade.jx
#pragma lifecycle(startup)
function startup() {
on('Camera::relocate', (event) => {
// Only react to the player's own camera, not to other cameras being moved.
if (event.camera !== CameraDirector.main) return;
const facing = event.yaw === undefined ? 'unchanged' : `${Math.round(RAD_TO_DEG(event.yaw))}°`;
log('Player arrived, facing', facing);
emit('hud/flash');
});
}

Every spawn, teleport, spot point and portal move ends with Camera::relocate. The script filters on the main camera so cameras moved for other reasons are ignored, converts the optional yaw from radians to degrees with RAD_TO_DEG, and emits a signal another script uses to flash the HUD.

  • Cutting to a camera that does not exist. switchTo throws. Check names, or wrap the call in try/catch when the camera may be absent.
  • Forgetting to cut back. A cut lasts until switchToMain() or until the camera goes away. Always plan the way back.
  • The player walks off during a cutscene. Player input stays live during a cut. Disable the Character Controller, then enable it again afterwards.
  • Trying to emit('Camera::relocate'). It is a system event: listen to it, never emit it.
  • Missing MotionPathController::end. The event is sent only with repeat: 'once', and only through a Signal Emitter on the camera.