In Lesson 4, we defined controls in the InputMap, rebuilt the character as a CharacterBody2D, and made it stand on a floor with gravity. In this lesson, the game finally gets its "hop." We'll add jumping, replace the floor with a tilemap to build a stage wider than the screen, and have a camera follow the player.
Finished project for Lesson 5: project-lesson05.zip (8 KB)
Add jumping
We'll add jumping to the player.gd from Lesson 4. We'll also remove the last two lines that kept the character on screen, since we're building walls in this lesson. Here's the whole thing:
extends CharacterBody2D
# Left/right movement speed (pixels per second)
@export var speed: float = 300.0
# Gravity strength: the downward speed increases by this much every second
@export var gravity: float = 1200.0
# Speed at the moment of the jump. Up on screen is negative, so this is a negative value
@export var jump_velocity: float = -600.0
# Called on every physics step (60 times per second by default)
func _physics_process(delta: float) -> void:
# When not standing on the floor, increase the downward speed a little at a time (this is gravity)
if not is_on_floor():
velocity.y += gravity * delta
# The moment the jump button is pressed, jump if we're on the floor
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
# If the button is released while still rising, halve the upward speed (short hop)
if Input.is_action_just_released("jump") and velocity.y < 0.0:
velocity.y *= 0.5
# -1 for left, 1 for right, 0 if neither is pressed
var direction: float = Input.get_axis("move_left", "move_right")
velocity.x = direction * speed
# Move according to velocity. If we hit a floor or wall, stop there
move_and_slide()How jumping works
A jump is just "give it upward speed all at once." Setting the y speed to -600 (upward) with velocity.y = jump_velocity launches the character upward. After that, gravity reduces the upward speed a little every step. The point where it reaches 0 is the top of the jump, and from there it starts to fall. The gravity code draws the arc for you.
With a speed of 600 and gravity of 1200, the jump reaches a height of 600 × 600 ÷ (2 × 1200) = 150 pixels. The tiles we make in this lesson are 64 pixels each, so that's just enough to reach a platform 2 tiles up (128 pixels).
Input.is_action_just_pressed("jump")istrueonly once, at the moment you press. If you usedis_action_pressed(which staystruethe whole time you hold the button), holding the button would make the character keep bouncing every time it lands.- Because of
and is_on_floor(), you can't jump in midair. Without it, you could jump again and again in the air.
Short hops (jump height depends on how long you hold)
Input.is_action_just_released("jump") is true at the moment you release the button. If you release while still rising (velocity.y < 0.0), we cut the upward speed in half. That way a quick tap gives a low jump and a long press gives a high one. It's a trick many action games use to make the controls feel good. If you don't want it, you can delete those three lines and it still works.
Create a TileSet
The floor in Lesson 4 was a single rectangle. Making a ColorRect and a collision shape every time you widen the stage or add a platform gets tedious. That's where a tilemap comes in. You build a stage by stamping small pictures (tiles) onto a grid, and the tiles can carry collision too.
In Godot 4.6, you use the TileMapLayer node (the TileMap node you'll see in older articles is deprecated). The bundle of tile images and settings is called a TileSet.
Get the tile image
This is a single image with three 64 × 64 pixel tiles made for this series: grass, dirt, and block. Download it and put it in your project folder (you can also drag it into the FileSystem dock).
Tile image: tiles.png (192 × 64 pixels)
Create the TileMapLayer and TileSet
- Open
main.tscn, select theFlooryou made in Lesson 4, and press Delete to remove it. - Add a
TileMapLayeras a child ofMain. - Click "<empty>" next to Tile Set in the Inspector and choose New TileSet.
- Click the new TileSet to expand it and set Tile Size to
64,64. - In the same place, expand Physics Layers and click Add Element. This creates "Physics Layer 0," which lets you give tiles collision.
Register the image and collision for the tiles
- Open the TileSet tab at the bottom of the editor. Drag
tiles.pngfrom the FileSystem dock into the Tile Sources list on the left. - When it asks "Auto Create Tiles in Non-Transparent Texture Regions?", click Yes. The image is split every 64 × 64, creating three tiles.
- Click Paint at the top of the middle column, then use the button under Paint Properties: (it says "Select a property editor" at first) to choose Physics Layer 0.
- An area for editing the collision shape (polygon) appears. If it isn't a square covering the whole tile, choose Reset to default tile shape from that area's menu.
- Click each of the three tiles in the tile image on the right. If a light blue square (the collision) appears over a tile you clicked, it's registered.
You can see the tiles, but the character falls right through them. This is the most common tilemap pitfall. The cause is almost always one of two things: you didn't add a physics layer, or you didn't click the tiles to "paint" collision onto them.
Paint tiles to build the stage
- With
TileMapLayerstill selected in the Scene dock, switch to the TileMap tab at the bottom of the editor. - On the Tiles tab, click the tile you want to use.
- Click or drag in the 2D view to place tiles on the grid. Right-click to erase. For large areas, drag with the rectangle tool (Rect) in the toolbar above to fill them in one go.
The stage in the finished project is 40 tiles wide and 11 tiles tall (2560 × 704 pixels). Tile coordinates start at (0, 0) in the top-left and count right and down.
| Where | Tile | Cells (x, y) |
|---|---|---|
| Ground surface | Grass | x = 0–39, y = 8 |
| Underground | Dirt | x = 0–39, y = 9–10 |
| Left and right walls | Block | x = 0 and x = 39, y = 0–7 |
| Low platforms (2 tiles above the ground) | Block | At y = 6: x = 6–9, 17–20, 24–27, 33–35 |
| High platforms (2 more tiles up) | Block | At y = 4: x = 11–13, 29–31 |
You don't have to copy this exactly. As long as you follow two rules, any layout is fine: a jump reaches at most 2 tiles up, and to walk under a low platform you need 1 empty tile between it and the ground.
Finally, move the player near the left wall. Set Player's Position to 160, 480. The title text in the top-left now overlaps the wall, so shift Title's Layout → Transform → Position x to 96.
Make the camera follow the player
The stage is now wider than the screen (1152 pixels), so as it stands, walking right takes the character off screen. If you make a Camera2D a child of the player, the camera moves with the player and keeps it in the center of the screen.
- Open
player.tscnand add aCamera2Das a child ofPlayer. - Expand Limit in the Inspector and set Left to
0, Top to0, Right to2560, and Bottom to648. - Expand Position Smoothing and turn on Enabled.
Limit is "the area the camera is allowed to show." Without it, at the edges of the stage the camera would show what's outside the stage (empty sky). The Right value of 2560 is the stage width (40 tiles × 64). Bottom is set to 648 (the screen height), so this stage doesn't scroll vertically.
With Position Smoothing on, the camera follows the player smoothly with a slight delay instead of sticking to it exactly. The screen shakes less and is easier to watch.
Limit depends on the size of the stage, so strictly speaking it's cleaner to set it on the stage side. This series only has one stage, so we put it in the player scene to keep things simple. When you add more stages, either place a camera in each stage or set Limit from a script.
Collision layers and masks
In the next lesson, we'll give the coins and enemies collision too. When we do, we'll want the player to collide differently depending on what it touches: for example, instead of bumping into and stopping at a coin, it should collect it on touch. The system for that is collision layers and collision masks.
- Layer: which group an object is in
- Mask: which groups an object looks at (collides with or detects)
Numbers alone are hard to remember, so let's name them first.
- In Project → Project Settings..., on the "General" tab, choose Layer Names → 2D Physics from the list on the left (if you can't find it, turn on Advanced Settings in the top right).
- Enter
worldfor Layer 1,playerfor Layer 2,coinfor Layer 3, andenemyfor Layer 4.
Next, decide which group each thing belongs to.
| Thing | Layer (where it is) | Mask (what it looks at) | Where to set it |
|---|---|---|---|
| Ground and platforms | 1 world | — | TileSet's Physics Layers (already 1 by default) |
| Player | 2 player | 1 world | Player → Collision in player.tscn |
| Coins and enemies | 3 coin / 4 enemy | 2 player | Next lesson |
Select Player in player.tscn and expand CollisionObject2D → Collision in the Inspector. In the Layer grid, click 1 to turn it off and turn 2 on. Leave only 1 on for Mask. Hover over a grid cell to see the name you gave it.
Now "the player only collides with the ground and platforms." Nothing looks different yet, but it will matter when we add coins and enemies in the next lesson. For details, see The complete guide to collision layers and masks in Godot 4.
Finishing touches: coins and background color
Let's scatter the coins across the whole stage. Select Coin3 and press Ctrl + D three times to make six coins. In the finished project, they're placed as follows. Putting them on platforms, or at heights you can only reach by jumping, makes it more fun.
| Coin | Position | Where |
|---|---|---|
| Coin | (480, 330) | On the first low platform |
| Coin2 | (800, 200) | On the high platform |
| Coin3 | (1216, 330) | On the second low platform |
| Coin4 | (900, 460) | On the ground |
| Coin5 | (1952, 200) | On the second high platform |
| Coin6 | (2208, 330) | On the last low platform |
Last, make the background sky-colored. On the "General" tab of Project Settings, type clear color in the search box at the top, and Rendering → Environment → Default Clear Color shows up. Set it to any light blue you like (the finished project uses Color(0.4, 0.62, 0.85), or 669ed9 in hex).
Run it with F5. Space jumps, and a quick tap gives a short hop. If you can hop onto platforms, the camera follows as you go right, and you stop at the wall on the right end, you're done.
- Set Jump Velocity to
-800on Player inplayer.tscn. Now you can reach 3 tiles up. Try changing gravity too, for a "jumps high but falls fast" feel. - Add more platforms: build some stairs, or spots where you have to jump to get a coin.
- Set the Camera2D's Zoom to
1.5,1.5to make the character look bigger.
Common pitfalls
| Symptom | Cause and fix |
|---|---|
| You can see the tiles, but the character falls through them | The TileSet has no physics layer, or you didn't paint collision onto the tiles. Go through "Register the image and collision for the tiles" again. You can also tell by turning on Debug → Visible Collision Shapes and checking whether light blue squares appear on the tiles. |
| The player falls through the ground (the tiles do have collision) | 1 is turned off in Player's Mask. Turn on Mask 1 (world). |
| You can't jump | Check that Space is registered to jump in the InputMap and that your code spells it "jump". |
| You can jump over and over in midair | and is_on_floor() is missing. |
| The camera doesn't follow | Check that the Camera2D is a child of Player. If it's in main.tscn, it won't move with the player. |
| The tiles are too small or misaligned | Check that the TileSet's Tile Size is 64 × 64. |
For more tilemap features (terrain auto-tiling, multiple layers, patterns, and so on), see The complete guide to tilemaps in Godot 4.
Working with AI
Jump code is easy work for AI, but tilemaps are mostly done in the editor, so how much you can hand off depends a lot on your setup.
- A (chat): it's good at the jump code and the "height math." You'll place the tiles by hand, but it makes a good partner for brainstorming "where should platforms go to make this fun?"
- B (Claude Code, etc.): it can write the code directly. But tile layouts are stored in the
.tscnin an unreadable form,tile_map_data = PackedByteArray(...), so building a stage by editing the file isn't realistic. To carry out the second prompt, you'd have to take a detour: have it write a@toolscript that builds the stage, then run that in the editor. - C (+ MCP): Godot MCP Pro has tools for placing tiles (
tilemap_set_cell,tilemap_fill_rect) and for reading the tilemap's state (tilemap_get_info,tilemap_get_used_cells), so the second prompt works as is. In the same flow, it can name the physics layers (set_physics_layers) and add the camera (add_node), then run the game (play_scene), press jump (simulate_action), and take a screenshot (get_game_screenshot) to check.
That said, you can't tell whether a stage is fun until you play it. Even with a stage the AI built, jump around yourself and fix things as you go: "this platform is too far," "a coin here would be nice."
Recap and what's next
- Jumping is just setting
velocity.yto a negative value. Gravity draws the arc from there is_action_just_pressedfires the moment you press;is_action_just_releasedfires the moment you release- Build stages with
TileMapLayerandTileSet. Collision comes from the TileSet's physics layer - Make
Camera2Da child of the player and it follows along. Limit sets the area it shows - Layer is "the group I'm in," Mask is "the groups I look at." Naming them makes things clearer
Next time: signals. We'll turn the coins into something that "disappears and adds to your score when you touch it," and add enemies that walk around the stage, so it starts to feel like a real game.
Want AI to help with placing tiles and playtesting too?
With Godot MCP Pro, Claude Code or Cursor can place tiles in a tilemap, run the game, jump, and check the result with screenshots (190 tools, $15 one-time purchase). You can finish this series without MCP.
Learn more about Godot MCP Pro · The complete guide to tilemaps in Godot 4