Jump with Space
This example jumps from one flat floor, falls under gravity, and allows another jump after landing. It works with a rigid body or a jelly body. For slopes, moving platforms and coyote time, use Platformer Controller and its own Jump binding.
Set up input and physics
- Open Settings → Project Input → Gameplay → Jump · Space. If Jump already exists, check that it is a Button action with a Space binding.
- Give the player Rigid Body 2D (Dynamic, Gravity Scale 1, Lock Rotation on) and Collider 2D. For a jelly, use Soft Body 2D with Gravity Scale 1 and no pinned particles instead; do not add a rigid body to it.
- Create a wide floor below the player. Add Collider 2D, with Sensor off. A collider without a rigid body acts as a fixed floor. Start the player slightly above it.
- In Scripts → New Behavior, replace the starter class with the complete source below and Save & Apply.
- In Arrange, add Jump to the player. Set its Ground property to the floor object. Leave Jump Speed at 420 to start.
- Press Play, click the game to focus it, let the player land, and press Space. Release and press again after landing to jump again.
Complete script
class Jump : Component {
[Property] float JumpSpeed = 420;
[Property] Entity Ground;
bool grounded;
void Update(float dt) {
if (!grounded || !Input.Pressed("Jump")) return;
RigidBody2D body = GetComponent<RigidBody2D>();
SoftBody2D jelly = GetComponent<SoftBody2D>();
if (body != null) body.SetVelocity(new Vector2(body.Velocity.X, -JumpSpeed));
else if (jelly != null) jelly.SetVelocity(new Vector2(0, -JumpSpeed));
grounded = false;
}
void OnCollisionStart(Entity other) {
if (Ground != null && other == Ground && Transform.WorldPosition.Y < Ground.WorldPosition.Y) grounded = true;
}
void OnCollisionEnd(Entity other) {
if (other == Ground) grounded = false;
}
}Input.Pressed("Jump") reads the action named Jump, not a physical key. It is true once per press. SetVelocity uses pixels per second: negative Y moves up. Unlike an impulse, the same velocity produces the same initial speed regardless of mass. The rigid-body branch preserves horizontal velocity; the soft-body branch sets it to zero.
The contact hooks enable jumping on the selected floor and disable it when leaving. This is a single-floor example, not general ground detection. ContactCount includes walls and ceilings and should not be treated as “standing on the ground.”
Find the broken step
| What happens | Check |
|---|---|
| Input's Jump meter does not react during Play | Focus the game, release Space, and press again. Verify Jump is a Button with a Space binding in Gameplay. |
| The meter reacts but the player stays still | Jump is attached and enabled; Ground points to the floor; both colliders are non-sensors; the rigid body is Dynamic or the soft body has movable particles. |
| The body moves only a tiny amount with an impulse | Impulse depends on mass. This recipe uses SetVelocity for a predictable starting speed. |
| The body falls through the floor | The floor needs Collider 2D, Sensor off. A rigid-body player also needs Collider 2D. |
| It works until another script is enabled | Another controller or script may replace velocity or position each frame. Choose one movement owner. |
API reference
- Input: Pressed, Held, Released, Axis and Vector
- RigidBody2D: velocity, impulse and body modes
- SoftBody2D: velocity, impulse and live solver values
- Component: lifecycle and collision hooks