Respawn objects that fall off the world
Updated
A ball rolls down a ramp and off its end; once it is 10 m down, teleport puts its body back at its starting place, and it rolls again.
Watch the ball roll off the ramp, fall and come back to the top. Drag to orbit the camera, scroll to zoom.
Respawn objects that fall off the world
#pragma lifecycle(startup, update)
const KILL_HEIGHT = -10;
let body;let home;let homeRotation;
// On a dynamic ball with a Rigid Body and a Sphere Collider.function startup() { body = self.parent.getComponent(PhysicsRigidBodyComponent); home = self.parent.position.clone(); homeRotation = self.parent.quaternion.clone();}
function update() { if (self.parent.position.y > KILL_HEIGHT) return; const landed = body.teleport(home, homeRotation); log('Respawned at', landed); emit('ball/respawned', { id: self.parent.entityId });}How it works
Section titled “How it works”The script is explained step by step in Respawn objects that fall off the world.
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.
BallRespawn.xrproj