A label that follows the selection
Updated
A text label with a Follow Target: selecting a moving exhibit retargets the follower to it and shows the exhibit’s name; deselecting hides it.
Click an exhibit, then another, and watch the label follow it as it moves. Drag to orbit the camera, scroll to zoom.
A label that follows the selection
#pragma lifecycle(startup)
function startup() { const label = findEntity('Inspect Label'); const follow = label.getComponent(FollowTargetComponent); const text = label.getComponent(TextComponent);
// Move the exhibits, so the label has something to keep up with. const glide = { duration: 3, ease: Ease.InOutSine, repeat: Infinity, yoyo: true }; Tween.to(findEntity('Vase').position, { z: -1.5 }, glide); Tween.to(findEntity('Bust').position, { y: 1.3 }, { ...glide, duration: 2 }); Tween.to(findEntity('Relic').position, { z: 1.5 }, { ...glide, duration: 4 });
on('exhibits', (event) => { if (event.eventType === EventType.Selectable.Select) { const exhibit = findEntity(event.entityId); follow.setProps({ target: exhibit.entityId, offset: { x: 0, y: 1.4, z: 0 }, lockY: true, positionTolerance: 0.05, // metres it may lag before it follows smoothTime: 0.2, // seconds to catch up }); text.setProps({ text: exhibit.name }); label.setEnabled(true); } if (event.eventType === EventType.Selectable.Deselect) { follow.setProps({ target: '' }); label.setEnabled(false); } });}How it works
Section titled “How it works”The script is explained step by step in A label that follows the selection.
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.
InspectLabel.xrproj