A moving platform
Updated
A kinematic platform slides back and forth with setKinematicTarget every frame, carrying the dynamic crates that rest on it.
Watch the platform carry the crates to and fro. Grab a crate and drop it back on. Drag elsewhere to orbit the camera.
A moving platform
#pragma lifecycle(startup, update)
const TRAVEL = 4; // metres from one end to the otherconst PERIOD = 6; // seconds for a full back-and-forth
let body;let start;let turn;const target = new THREE.Vector3();
// The platform has a Rigid Body with type 'kinematic' and a Box Collider.function startup() { body = self.parent.getComponent(PhysicsRigidBodyComponent); start = self.parent.position.clone(); turn = self.parent.quaternion.clone();}
function update(dt, time) { const t = (Math.sin((time / PERIOD) * M_TAU) + 1) / 2; // 0 → 1 → 0 target.copy(start); target.x += t * TRAVEL; body.setKinematicTarget(target, turn);}How it works
Section titled “How it works”The script is explained step by step in A moving platform.
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.
MovingPlatform.xrproj