Zoom, pan and pick on a top-down map
Updated
An Orthographic Camera looks straight down on a little town, with no perspective: the mouse wheel changes its size, the height of the view, the arrow keys move it across the map, and a click selects a house, shown with an animated outline.
Turn the mouse wheel over the map to zoom between 5 and 60 m of view height. Click a house to select it, then hold the arrow keys to move across the map.
Zoom, pan and pick on a top-down map
#pragma lifecycle(startup, update)
const MIN_SIZE = 5;const MAX_SIZE = 60;const PAN_SPEED = 0.5; // view heights per second
let mapCamera;let map;let size;
function startup() { mapCamera = self.parent; map = mapCamera.getComponent(OrthographicCameraComponent); size = map.props.size;
// Selection is single: picking a house deselects the last one, which turns its outline off. on('map/pick', (event) => { const picked = findEntity(event.entityId); picked.getComponent(OutlineComponent).setEnabled(event.data.selected); if (event.data.selected) log('Selected', picked.name); });}
function update(dt) { const wheel = Input.mouse.wheelDelta(); if (wheel !== 0) { // Scrolling up (positive) zooms in: a smaller view height. size = Math.min(MAX_SIZE, Math.max(MIN_SIZE, size * (1 - wheel * 0.001))); map.setProps({ size }); }
// The camera looks straight down, so up on screen is -Z in the world. const keys = Input.keyboard; const x = (keys.isKeyPressed(Key.ArrowRight) ? 1 : 0) - (keys.isKeyPressed(Key.ArrowLeft) ? 1 : 0); const z = (keys.isKeyPressed(Key.ArrowDown) ? 1 : 0) - (keys.isKeyPressed(Key.ArrowUp) ? 1 : 0); mapCamera.position.x += x * size * PAN_SPEED * dt; mapCamera.position.z += z * size * PAN_SPEED * dt;}How it works
Section titled “How it works”The script is explained step by step in Zoom, pan and pick on a top-down map.
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.
MapZoom.xrproj