Jump only while touching the ground
Updated
A ball counts its solid contacts from collision-enter and collision-exit, and Space kicks it upwards only while it touches something, so it cannot jump again in mid-air.
Click the scene, then press Space to make the ball jump. Pressing again in mid-air does nothing. Drag to orbit the camera, scroll to zoom.
Jump only while touching the ground
#pragma lifecycle(startup, update)
let body;let contacts = 0;
// On a dynamic Ball with a Rigid Body, a Sphere Collider and a Signal Emitter with the signal 'ball'.function startup() { body = self.parent.getComponent(PhysicsRigidBodyComponent);
on('ball', (event) => { if (event.entityId !== self.parent.entityId) return; if (event.eventType === EventType.PhysicsCollider.CollisionEnter) contacts += 1; if (event.eventType === EventType.PhysicsCollider.CollisionExit) contacts = Math.max(0, contacts - 1); });}
function update() { if (contacts > 0 && Input.keyboard.isKeyReleased(Key.Space)) { body.applyImpulse({ x: 0, y: 3, z: 0 }); }}How it works
Section titled “How it works”The script is explained step by step in Jump only while touching the ground.
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.
GroundedJump.xrproj