A radio you switch on
Updated
Click a radio to switch its music on and off; while it plays, it drives 10 m away and back, and the music fades to silence and returns: a Positional Audio with a linear fade under a Selectable mesh, played and paused with its trip by a Behavior script.
With your sound on, click the radio: the music fades to silence as it drives away and comes back as it returns. Click it again to stop both.
A radio you switch on
#pragma lifecycle(startup)
let sound;let trip;
function startup() { const radio = findEntity('Radio'); sound = findEntity('Radio Sound').getComponent(PositionalAudioComponent);
// 10 m away and back, for ever: paused until the music plays. trip = Tween.to(radio.position, { z: radio.position.z - 10 }, { duration: 5, ease: Ease.InOutSine, repeat: Infinity, yoyo: true, }); trip.pause();
on('radio/clicked', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again if (sound.isPlaying()) { sound.pause(); trip.pause(); } else { sound.play(); trip.resume(); } });}How it works
Section titled “How it works”The script is explained step by step in A radio you switch on.
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.
RadioSwitch.xrproj