Carry a crate
Updated
E switches a crate between kinematic, led in front of the camera with setKinematicTarget, and dynamic, falling freely; its collider is kept.
Click the scene, then press E to pick the crate up, orbit the camera, and press E again to drop it.
Carry a crate
#pragma lifecycle(startup, update)
const HOLD = new THREE.Vector3(0, 1.2, -1.5); // in front of the cameraconst held = new THREE.Vector3();const facing = new THREE.Quaternion();
let crate;let body;let carrying = false;
function startup() { crate = findEntity('Crate'); body = crate.getComponent(PhysicsRigidBodyComponent);}
function update() { if (Input.keyboard.isKeyReleased(Key.E)) { carrying = !carrying; body.setProps({ type: carrying ? 'kinematic' : 'dynamic' }); if (!carrying) body.wakeUp(); } if (!carrying || !camera) return;
held.copy(HOLD).applyMatrix4(camera.matrixWorld); camera.getWorldQuaternion(facing); body.setKinematicTarget(held, facing);}How it works
Section titled “How it works”The script is explained step by step in Carry a crate.
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.
CarryCrate.xrproj