Zoom a scope while the right button is held
Updated
While the right mouse button is held, a script on the camera eases its Perspective Camera’s field of view from 60° to 20°, and back on release.
Hold the right mouse button to zoom in on the far target, and release it to zoom out. Drag to orbit the camera.
Zoom a scope while the right button is held
#pragma lifecycle(startup, update)
const WIDE = 60; // degreesconst ZOOM = 20; // degreesconst SPEED = 8; // how fast the lens eases, per second
let lens;let fov = WIDE;
function startup() { // This script is on the player's Perspective Camera. 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 });}How it works
Section titled “How it works”The script is explained step by step in Zoom a scope while the right button is held.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
ScopeZoom.xrproj