Make Games with Godot 4 and AI · Lesson 5 of 11

Godot 4 Jumping and Tilemaps with a Camera (Lesson 5)

Time to jump. You'll replace the floor with a tilemap to build a wide stage with platforms, and have a camera follow the player. To prepare for the next lesson, we'll also sort out collision layers and masks.

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)

The finished game from Lesson 5. Grass-and-dirt ground stretches under a light blue sky, with orange block platforms and coins. The character is mid-jump
Where we'll end up in this lesson. You can hop onto platforms as you make your way to the right

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:

player.gd
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") is true only once, at the moment you press. If you used is_action_pressed (which stays true the 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)

The tile image. From left: dirt with grass on top, dirt, and an orange block
From left: "grass," "dirt," and "block." Feel free to redraw them however you like

Create the TileMapLayer and TileSet

  1. Open main.tscn, select the Floor you made in Lesson 4, and press Delete to remove it.
  2. Add a TileMapLayer as a child of Main.
  3. Click "<empty>" next to Tile Set in the Inspector and choose New TileSet.
  4. Click the new TileSet to expand it and set Tile Size to 64, 64.
  5. 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

  1. Open the TileSet tab at the bottom of the editor. Drag tiles.png from the FileSystem dock into the Tile Sources list on the left.
  2. When it asks "Auto Create Tiles in Non-Transparent Texture Regions?", click Yes. The image is split every 64 × 64, creating three tiles.
  3. 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.
  4. 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.
  5. 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.
It's easy to forget the collision

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

  1. With TileMapLayer still selected in the Scene dock, switch to the TileMap tab at the bottom of the editor.
  2. On the Tiles tab, click the tile you want to use.
  3. 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 editor with TileMapLayer selected. The 2D view shows grass ground and block platforms, and the TileMap panel at the bottom shows the three tiles
Selecting a TileMapLayer opens the TileMap panel at the bottom. Pick tiles here and place them in the 2D view

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.

WhereTileCells (x, y)
Ground surfaceGrassx = 0–39, y = 8
UndergroundDirtx = 0–39, y = 9–10
Left and right wallsBlockx = 0 and x = 39, y = 0–7
Low platforms (2 tiles above the ground)BlockAt y = 6: x = 6–9, 17–20, 24–27, 33–35
High platforms (2 more tiles up)BlockAt 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.

  1. Open player.tscn and add a Camera2D as a child of Player.
  2. Expand Limit in the Inspector and set Left to 0, Top to 0, Right to 2560, and Bottom to 648.
  3. 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.

Is it OK to set camera limits in the player scene?

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.

  1. 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).
  2. Enter world for Layer 1, player for Layer 2, coin for Layer 3, and enemy for Layer 4.

Next, decide which group each thing belongs to.

ThingLayer (where it is)Mask (what it looks at)Where to set it
Ground and platforms1 world—TileSet's Physics Layers (already 1 by default)
Player2 player1 worldPlayer → Collision in player.tscn
Coins and enemies3 coin / 4 enemy2 playerNext 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.

CoinPositionWhere
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.

Try it
  • Set Jump Velocity to -800 on Player in player.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.5 to make the character look bigger.

Common pitfalls

SymptomCause and fix
You can see the tiles, but the character falls through themThe 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 jumpCheck that Space is registered to jump in the InputMap and that your code spells it "jump".
You can jump over and over in midairand is_on_floor() is missing.
The camera doesn't followCheck 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 misalignedCheck 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

If you hand this lesson's work to 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.

This is a CharacterBody2D in Godot 4.6. Add jumping to player.gd using the jump action. The player should only be able to jump while on the floor, and releasing the button early should give a lower jump. I want it to reach a platform 2 tiles (128 pixels) up, so calculate jump_velocity for a gravity of 1200 and explain the math.
Build a stage 40 tiles wide and 11 tiles tall in the TileMapLayer in main.tscn. Put grass (atlas coords 0,0) at y=8, dirt (1,0) at y=9–10, walls (2,0) at x=0 and x=39 for y=0–7, plus a few platforms. Keep the platforms no more than 2 tiles up so they can be reached with a jump.
  • 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 .tscn in 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 @tool script 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.y to a negative value. Gravity draws the arc from there
  • is_action_just_pressed fires the moment you press; is_action_just_released fires the moment you release
  • Build stages with TileMapLayer and TileSet. Collision comes from the TileSet's physics layer
  • Make Camera2D a 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

All lessons in the series (11)
  1. Lesson 1: Installing Godot and setting up AI tools
  2. Lesson 2: Nodes and scenes
  3. Lesson 3: GDScript basics
  4. Lesson 4: Input and player movement
  5. Lesson 5: Gravity, jumping, and tilemapsYou are here
  6. Lesson 6: Signals, coins, and enemies
  7. Lesson 7: UI and screen transitions
  8. Lesson 8: Sound and game feel
  9. Lesson 9: Letting AI playtest your game
  10. Lesson 10: Exporting and publishing
  11. Lesson 11: Bonus: one-prompt games by genre