HomeGuidesRecipesDocumentationChangelog
Log InDiscussions
Discussions

Set Active Camera

Set the player camera to a specific camera node

Definition

setActiveCamera(cameraId: string)

The unload method cleans up an active player / configurator

ParameterTypeDescription
cameraIdstringThe node ID for a camera node.
player.setActiveCamera('d2b281c3-cb60-4b1c-b27a-b53600929ea3');

Usage

This method is typically used when the player needs to switch the camera to a different vantage point. Although that can be accomplished through a transform of the camera's position, it is more convenient to define multiple cameras in a scene and switch between them.

The method can be used either on the front-end or inside custom code rules in the configuration setup. Switching between cameras is typically performed as part of configuration rules. We do not recommend performing this function directly on the front end, unless it is not possible to do it from the configuration rules for some reason.

One example of where this function might be needed in the configuration rules would be if the target camera exists only inside nested assets, as opposed to directly available within the scene. For more details visit the camera docs and the scene API.

// Search for a specific camera node in the hierarchy, and set it as the active camera
// This exaple uses the api object instead of the player object, because this is how we 
// need to reference it from within custom code rules

const camNode = api.scene.findNode({hierarchical: true, from: api.instanceId, type: 'Camera', name: 'MyCustomCamera'});
if (camNode) { api.setActiveCamera(camNode); }

๐Ÿ“˜

Transitioning from one camera position to another will happen instantly using this method. This method would not be suitable in case where a gradual animated transition is necessary.