Cat and mice
Updated
An amber cat with a Steering Pursue chases the nearest of four mice, cutting them off, while each mouse uses a Steering Evade to dodge where the cat is heading.
Watch the cat cut the mice off and the mice veer away once it comes within 3 m; the label names its current prey. Drag to orbit the camera, scroll to zoom.
Cat and mice
#pragma lifecycle(startup, update)
const MICE = ['Mouse 1', 'Mouse 2', 'Mouse 3', 'Mouse 4'];const cat = { entity: null, pursue: null };const mice = [];let label;let prey = '';let timer = 0;
function startup() { cat.entity = findEntity('Cat'); cat.pursue = cat.entity.getComponent(SteeringPursueComponent); for (const name of MICE) mice.push(findEntity(name)); label = findEntity('Chase').getComponent(UILabelComponent);}
// Twice a second, the cat switches its Pursue to the nearest mouse.function update(dt) { timer -= dt; if (timer > 0) return; timer = 0.5; let nearest = mice[0]; for (const mouse of mice) { if (mouse.position.distanceTo(cat.entity.position) < nearest.position.distanceTo(cat.entity.position)) nearest = mouse; } if (nearest.name === prey) return; prey = nearest.name; cat.pursue.setProps({ target: nearest }); label.setProps({ text: `The cat is chasing ${prey}` }); log('chasing', prey);}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.
CatAndMice.xrproj