Build a timeline from code
Updated
A gem hovers up and down and spins for ever on an Animation Timeline that a Behavior script builds in code, with a smooth pingpong layer and a linear repeating one.
Watch the gem hover and spin. Drag to orbit the camera, scroll to zoom.
Build a timeline from code
#pragma lifecycle(startup)
function startup() { const p = self.parent.position; const timeline = new AnimationTimelineComponent({ layers: [ { name: 'hover', tracks: [ { prop: 'position', type: 'vector', interpolation: 'smooth', keys: [ { time: 0, value: [p.x, p.y, p.z] }, { time: 1, value: [p.x, p.y + 0.3, p.z] }, ], }, ], }, { name: 'spin', tracks: [ { prop: 'rotation', type: 'rotation', interpolation: 'linear', keys: [ { time: 0, value: [0, 0, 0] }, { time: 4, value: [0, M_TAU, 0] }, ], }, ], }, ], }); self.parent.addComponent(timeline);
timeline.layer('hover')?.play({ repeat: 'pingpong' }); timeline.layer('spin')?.play({ repeat: 'repeat' });}How it works
Section titled “How it works”The script is explained step by step in Build a timeline from code.
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.
HoverSpinTimeline.xrproj