Move a box with the keyboard
Updated
Your first input script: the arrow keys or WASD move a box, multiplied by dt so the speed is the same at any frame rate, and Space makes it hop.
Click the view so it has the keyboard focus, then use the arrow keys or WASD, and Space to hop.
Move a box with the keyboard
#pragma lifecycle(startup, update)
const SPEED = 3; // metres a secondlet box;
function startup() { box = findEntity('Player Box');}
function update(dt) { const keys = Input.keyboard; if (keys.isKeyPressed(Key.ArrowLeft) || keys.isKeyPressed(Key.A)) box.position.x -= SPEED * dt; if (keys.isKeyPressed(Key.ArrowRight) || keys.isKeyPressed(Key.D)) box.position.x += SPEED * dt; if (keys.isKeyPressed(Key.ArrowUp) || keys.isKeyPressed(Key.W)) box.position.z -= SPEED * dt; if (keys.isKeyPressed(Key.ArrowDown) || keys.isKeyPressed(Key.S)) box.position.z += SPEED * dt;
// Released is true on one frame only, so each press hops once. if (keys.isKeyReleased(Key.Space)) box.position.y += 0.5;}How it works
Section titled “How it works”The script is explained step by step in Move a box with the keyboard.
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.
FirstInput.xrproj