Appearance
Camera Beta
scene.camera points the camera at objects in the scene. Follow a character as it moves, turn toward one object after another for a guided tour, then hand the camera back to the viewer.
Where it applies
The camera follows a script only where the experience drives its own camera: on the Mac, and in the preview outside AR on iPhone and iPad. Read scene.camera.kind to find out which case you are in:
kind | Where | What the calls do |
|---|---|---|
'orbit' | Mac, and the preview outside AR | Take effect right away. |
'device' | AR on iPhone and iPad | The device is the camera. Calls are kept and take effect once the view leaves AR, with one console warning. |
'device' | Apple Vision Pro | The wearer's head is the camera. follow and lookAt return false with a warning. |
kind changes when the preview switches in or out of AR. environment.features.has('cameraControls') is false on Apple Vision Pro, so gate a portable script on the flag:
javascript
// Attach to: On Experience Start
if (environment.features.has('cameraControls')) {
scene.camera.follow(scene.findEntity({ name: 'Hero' }));
}Units
Framing angles (azimuth, elevation, elevationRange) are in degrees. Distances and offsets are in metres, damping in seconds. Rotation values elsewhere in scripting are radians, so don't pass one here.
Follow a character
follow keeps an object in view. The camera's orbit moves along with the object, and the viewer can still drag or use the right stick to swing around it:
javascript
// Attach to: On Experience Start
var hero = scene.findEntity({ name: 'Hero' });
scene.camera.follow(hero, {
distance: 1.6, // metres from the aim point
elevation: 30, // degrees above the horizon
offset: Vector3(0, 0.1, 0), // aim 10 cm above the object's origin
verticalDamping: 2, // seconds; slower in height, so a jump doesn't bob the view
elevationRange: [10, 50], // degrees the viewer can orbit between
zoom: { min: 0.8, max: 3 } // metres the viewer can zoom between
});While the camera follows, keys, two-finger pan and the left stick no longer move it. Clicking the right stick returns to the framing the follow started with.
| Option | Default | Description |
|---|---|---|
distance | as now | Metres from the camera to the aim point. |
azimuth | as now | Degrees around the object. 0 looks toward -z. |
elevation | as now | Degrees above the horizon. |
offset | Vector3(0, 0, 0) | World-space metres from the object's origin to the point the camera aims at, such as above a character's feet. |
damping | about 1.5 | Seconds the camera takes to settle after the object moves. 0 is rigid. |
verticalDamping | damping, or about 3.8 | The same for height. |
elevationRange | [0, 60] | [min, max] degrees the viewer can orbit between. |
orbit | true | Whether dragging and the right stick orbit. |
zoom | true | Whether pinch and scroll change the distance. Pass { min, max } in metres to limit it, or false to switch it off. |
Leave out distance, azimuth or elevation to keep the current view for that value.
Look at objects
lookAt turns the camera toward an object and keeps it turned there while the object moves. The camera stays where it is, or stands at from. Dragging, zooming and the right stick's click do nothing until release(). It takes from, offset and damping, with the same units and defaults as follow.
A guided tour, where the viewer drives the camera again at the end:
javascript
// Attach to: On Experience Start
var STOP_SECONDS = 4;
var VIEWPOINT = Vector3(0, 0.4, 1.2); // metres in front of and above each exhibit
if (scene.camera.kind === 'orbit') {
for (var i = 1; i <= 3; i++) {
var exhibit = scene.findEntity({ name: 'Exhibit ' + i });
scene.camera.lookAt(exhibit, { from: exhibit.worldPosition.add(VIEWPOINT), damping: 0.8 });
await wait(STOP_SECONDS);
}
scene.camera.release();
} else {
console.log('In AR the visitor walks the tour themselves');
}Hand the camera back
release() hands the camera back to the viewer from where it is now. scene.camera.mode reports what the last call set up: 'free', 'follow' or 'lookAt'.
A follow or look-at also ends on its own:
- when the scene changes
- when the object leaves the scene, with a console warning
mode then reads 'free' again.
Game controller sticks
Outside AR a game controller drives the camera. scene.camera.gamepad decides which of its inputs the camera keeps:
| Property | Inputs | Default |
|---|---|---|
move | The left stick and the triggers move the camera. | true |
orbit | The right stick orbits; its click resets the view. | true |
Set one to false to read those inputs in your own script:
javascript
// Attach to: On Experience Start
// The left stick walks the character; the right stick still orbits the camera.
scene.camera.gamepad.move = false;Both return to true when the scene changes. While the camera follows or looks at something, move has no effect anyway. See Game Controllers for reading the sticks.
Reference
| Member | Type | Description |
|---|---|---|
scene.camera.kind | string | 'orbit' where the experience drives its own camera, 'device' where the device is the camera. |
scene.camera.mode | string | 'free', 'follow' or 'lookAt'. |
scene.camera.gamepad | { move, orbit } | Which game controller inputs the camera keeps. |
scene.camera.follow(target, options) | boolean | Keep an object in view. true once the camera follows, including in AR. |
scene.camera.lookAt(target, options) | boolean | Turn toward an object. true once the camera looks at it, including in AR. |
scene.camera.release() | Hand the camera back to the viewer. |
target is an object or representation, such as the result of scene.findEntity(...).
Related
- scene.camera – the API reference
- Game Controllers – sticks, buttons and haptics
- Feature Detection – the full capability matrix
- Examples – a follow camera in a small game