Shake a springy stick
Updated
A script adds Dynamic Spring Bone components to the bones of a loaded stick, then swings the stick from side to side so its tip lags and whips behind.
Watch the tip lag behind. Click the scene and press Space to stop the shaking and watch the stick settle. Drag to orbit the camera, scroll to zoom.
Shake a springy stick
#pragma lifecycle(startup, update)
const stick = self.parent;let shaking = true;let phase = 0;
function startup() { // Softer toward the tip, so it whips. ['Bone.001', 'Bone.002'].forEach((bone, i) => { stick.addComponent(new DynamicSpringBoneComponent({ bone, stiffness: 300 - i * 120, damping: 6 })); }); log('Spring bones:', stick.getComponents(DynamicSpringBoneComponent).length);}
function update(dt) { if (Input.keyboard.isKeyReleased(Key.Space)) { shaking = !shaking; log(shaking ? 'Shaking' : 'Holding still'); } if (!shaking) return; phase += dt * 4; // only while shaking, so it resumes where it stopped stick.setPosition(Math.sin(phase) * 0.5, 0, 0);}How it works
Section titled “How it works”The script is explained step by step in Shake a springy stick.
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.
SpringyStick.xrproj