2D in Godot 4.7
Movement physics lives in godot-physics, UI in godot-ui, shaders in godot-shaders-vfx. Look up any API with ../godot-core/scripts/api_lookup.py.
Project setup
| Style | Settings |
|---|---|
| Pixel art | rendering/textures/canvas_textures/default_texture_filter = 0 (Nearest); display/window/stretch/mode = "viewport", scale_mode = "integer", aspect keep/expand; base size small (for example 320×180 or 640×360); optionally rendering/2d/snap/snap_2d_transforms_to_pixel = true |
| HD 2D / UI-heavy | stretch mode canvas_items (the 4.7 default for new projects), aspect expand, base size 1920×1080 or 1280×720, Linear filter |
| Never mix texel sizes in pixel-art games. Scale sprites by whole numbers only. |
TileMapLayer (TileMap is deprecated)
- Use one
TileMapLayernode per layer (ground, walls, decoration), all sharing oneTileSetresource. OldTileMapnodes convert through the TileMap panel's toolbox: "Extract TileMap layers". - Code:
set_cell(coords, source_id, atlas_coords, alternative),erase_cell,get_cell_source_id(−1 means empty),get_cell_atlas_coords,get_used_cells(),get_used_cells_by_id(),local_to_map(to_local(global_pos)),map_to_local(cell). - Per-tile metadata: add custom data layers to the TileSet (
is_hazard: bool,cost: float), then read them withget_cell_tile_data(cell).get_custom_data("is_hazard"). - Autotiling: set up terrain sets in the TileSet, then call
set_cells_terrain_connect(cells, terrain_set, terrain)orset_cells_terrain_path. - Collision, navigation and occlusion come from the TileSet's physics, navigation and occlusion layers. Physics shapes are merged into chunks (
physics_quadrant_size, default 16). Set it to 1 only if you needget_coords_for_body_ridper cell. - For changes at runtime, set the cells, then
update_internals()if you need them immediately. Per-cell runtime variations use_use_tile_data_runtime_updateand_tile_data_runtime_updatewithnotify_runtime_tile_data_update(). - Large worlds: use chunked layers per region and free distant chunks. Thousands of
set_cellcalls in one frame stall, so spread them over several frames.
Camera2D
position_smoothing_enabledandposition_smoothing_speedsmooth the follow. Limits arelimit_left/top/right/bottom(toggle them all withlimit_enabled).drag_*_enabledadds a dead zone.- Screen shake: animate
offset(notposition) with decaying noise. See the reference. - Use one active camera per viewport (
make_current()). Zoom useszoom(Vector2: >1 zooms in).
Parallax2D (replaces ParallaxBackground/Layer)
Add Parallax2D nodes anywhere, with a Sprite2D or TextureRect as child. Set scroll_scale (0 = static, 1 = moves with the world), repeat_size = texture size for infinite tiling, repeat_times for zoomed-out cameras and autoscroll for clouds. It follows the active Camera2D automatically.
Drawing order
y_sort_enabledon the parent sorts its children by Y (top-down games). Each child's sort origin is its own origin, so put sprite feet at y=0.z_index(relative to the parent unlessz_as_relative = false) works across siblings.CanvasLayerhandles HUD and backgrounds independently of the camera.CanvasGrouprenders a subtree as one unit, which lets outline shaders and transparency apply to a whole character.
Lights and atmosphere
PointLight2D (textured) and DirectionalLight2D, plus LightOccluder2D for shadows (TileSets can carry occluders). CanvasModulate darkens the scene. Normal and specular maps go through a CanvasTexture used as the sprite texture. Light2D is per-pixel, so keep lights overlapping any given sprite in the single digits on mobile.
Sprites and animation
AnimatedSprite2D+SpriteFrameshandles frame animation (play(&"run"),animation_finished). Since 4.7, per-animation loop mode usesSpriteFrames.set_animation_loop_mode.- Use
AnimationPlayerwhen frames must sync with hitboxes, sounds or method calls. Keyframe theframeproperty of aSprite2Dwithhframes/vframes. - Sprite sheets:
Sprite2D.region_enabled+region_rect, orAtlasTexture.
Platformer specifics
- One-way platforms:
CollisionShape2D.one_way_collision = true. 4.7 addsone_way_collision_direction(defaultVector2(0, 1)). Drop through by briefly disabling the player's mask bit or raising the position. CharacterBody2Ddefaults toMOTION_MODE_GROUNDEDfor platformers. UseMOTION_MODE_FLOATINGfor top-down.- Coyote time, jump buffering and variable jump height are in the reference.
Many objects
Thousands of bullets or particles: MultiMeshInstance2D or GPU particles, or skip nodes entirely with RenderingServer canvas items and PhysicsServer2D. VisibleOnScreenEnabler2D pauses off-screen enemies. See godot-performance.
Patterns (platformer feel, top-down movement, camera shake, tile queries, custom draw), compiled by the tests: references/patterns.md


