Walk across a gap
Updated
A hero walks back and forth between two platforms that do not touch: a NavMesh Link across the gap lets its path continue, and the script logs when the agent leaves the ground to cross it.
Watch the hero jump the gap on each crossing. Drag to orbit the camera, scroll to zoom.
Walk across a gap
#pragma lifecycle(startup, update)
const SPOTS = [{ x: -3, y: 0, z: 0 }, { x: 3, y: 0, z: 0 }];let character;let agent;let target = 1;let walking = false;let resting = false;let inAir = false;
function startup() { const hero = findEntity('Hero'); character = hero.getComponent(CharacterControllerComponent); agent = hero.getComponent(NavMeshAgentComponent);
on('hero', (event) => { if (event.eventType !== EventType.CharacterController.Arrive) return; log('Arrived on the', target === 1 ? 'right' : 'left', 'platform'); target = 1 - target; walking = false; resting = true; delay(() => { resting = false; }, 1000); });}
function update() { // moveTo is false until the agent has joined the navmesh, a few frames into play. if (!walking && !resting) walking = character.moveTo(SPOTS[target]);
if (agent.isGrounded === inAir) { inAir = !agent.isGrounded; log(inAir ? 'Jumping the gap' : 'Landed'); }}How it works
Section titled “How it works”The script is explained step by step in Walk across a gap.
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.
JumpTheGap.xrproj