Recenter a group built at run time
Updated
A script spawns four bricks under the Pile group, then calls recenter() on its Group component, so the pile spins around the middle of its bricks instead of its old origin.
Watch the bricks turn around their own centre; the log says how many entities recenter() moved. Drag to orbit the camera, scroll to zoom.
Recenter a group built at run time
#pragma lifecycle(startup, update)
let pile = null;
function startup() { pile = findEntity('Pile'); const group = pile && pile.getComponent(GroupComponent); if (!group) { log('Pile is not a Group entity'); return; }
for (let i = 0; i < 4; i++) { spawn('Brick', { parent: pile, position: { x: 2 + i * 0.6, y: 0.25, z: 1 }, enabled: true }); }
const moved = group.recenter(); log('Recentered; entities moved:', moved.length);}
function update(dt) { if (pile) pile.rotation.y += dt * 0.5;}How it works
Section titled “How it works”The script is explained step by step in Recenter a group built at run time.
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.
RecenterPile.xrproj