diff --git a/docs/user-manual/graphics/cameras/camera-controls.md b/docs/user-manual/graphics/cameras/camera-controls.md new file mode 100644 index 00000000000..7c0fff3d9eb --- /dev/null +++ b/docs/user-manual/graphics/cameras/camera-controls.md @@ -0,0 +1,114 @@ +--- +title: Camera Controls +description: Add orbit, fly, and pan navigation to any camera with the engine's ready-made camera-controls script, with mouse, touch, and gamepad support. +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +Most applications need some way for the user to move the camera. Rather than writing this from scratch, you can use the `CameraControls` script that ships with the engine at [`scripts/esm/camera-controls.mjs`](https://github.com/playcanvas/engine/blob/main/scripts/esm/camera-controls.mjs). It provides production-quality navigation with a single script: + +* **Orbit** — left mouse drag rotates around a focus point, right mouse drag pans, and the mouse wheel zooms. +* **Fly** — WASD keys move the camera freely while the mouse looks around. +* **Touch and gamepad** — multi-touch gestures and gamepad input are supported out of the box. + +Attach the script to a camera entity: + + + + +```javascript +// Load the script (here from the CDN; you can also bundle it with your app) +const asset = new pc.Asset('camera-controls', 'script', { + url: 'https://cdn.jsdelivr.net/npm/playcanvas/scripts/esm/camera-controls.mjs' +}); +app.assets.add(asset); +app.assets.load(asset); + +asset.ready(() => { + // Attach it to the camera entity + camera.addComponent('script'); + camera.script.create('cameraControls', { + properties: { + focusPoint: new pc.Vec3(0, 1, 0) + } + }); +}); +``` + +If you build your app with a bundler, you can import the script class directly instead: + +```javascript +import { CameraControls } from 'playcanvas/scripts/esm/camera-controls.mjs'; + +camera.addComponent('script'); +camera.script.create(CameraControls); +``` + + + + +Add `camera-controls.mjs` to your project as a script asset (copy it from the [engine repository](https://github.com/playcanvas/engine/blob/main/scripts/esm/camera-controls.mjs)). Then select your camera entity, add a [Script Component](/user-manual/editor/scenes/components/script) and add the **cameraControls** script to it. The script's attributes can then be tuned in the Inspector. + + + + +```jsx +import { CameraControls } from 'playcanvas/scripts/esm/camera-controls.mjs'; +import { Script } from '@playcanvas/react/components'; + + + +