Take the Parallax2D out of the CanvasLayer and put it on the main canvas, with a low z_index if it has to draw behind everything. Or keep the CanvasLayer and turn on follow_viewport_enabled (Follow Viewport > Enable). In our Godot 4.7 test the camera moved 400 px to the right. A Parallax2D with scroll scale 0.5 on the main canvas moved 200 px to the left on screen, as it should. The same layer inside a CanvasLayer at layer -1 moved 200 px to the right, with the camera. With Follow Viewport on, it moved 200 px left again.
The code
Save it as parallax_canvas.gd. wrong_layer() lists the Parallax2D nodes that sit in a CanvasLayer which does not follow the camera: print it when a background drifts. draw_behind() sets a Parallax2D to an absolute z_index of -100, so it draws behind the level on the main canvas. For example: const ParallaxCanvas := preload("res://parallax_canvas.gd"), print(ParallaxCanvas.wrong_layer(self)), then ParallaxCanvas.draw_behind($Sky) on each background layer.
extends RefCounted
## Parallax2D works out its scroll from the camera on the main canvas.
## These helpers find layers that sit where the camera does not reach, and keep a background behind without a CanvasLayer.
## Paths of the Parallax2D nodes under root that sit in a CanvasLayer which does not follow the camera.
static func wrong_layer(root: Node) -> Array[String]:
var found: Array[String] = []
for node in root.find_children("*", "Parallax2D", true, false):
var p := node as Parallax2D
var layer := p.get_canvas_layer_node()
if layer and not layer.follow_viewport_enabled and not p.ignore_camera_scroll:
found.append(str(p.get_path()))
return found
## Draws a Parallax2D behind everything on the main canvas, the job people move it into a CanvasLayer for.
static func draw_behind(p: Parallax2D, z := -100) -> void:
p.z_as_relative = false
p.z_index = z
What we measured
A Camera2D moved from x 480 to x 880, and we read where a 64 px square was drawn on screen before and after. Two runs gave the same numbers.
- A plain Sprite2D on the main canvas: moved -400 px, with the world.
- Parallax2D, scroll scale 0.5, on the main canvas: -200 px, half the speed of the world. Right.
- A plain Sprite2D in a CanvasLayer at layer -1: 0 px. A CanvasLayer does not follow the camera.
- The same Parallax2D in that CanvasLayer: +200 px, moving with the camera instead of against it. wrong_layer() listed it.
- The same CanvasLayer with Follow Viewport turned on: -200 px. wrong_layer() listed nothing.
- On the main canvas, a red player sprite added before the background: the background covered it. After draw_behind() the player showed in front, and the layer still moved -200 px.
Why it happens
Parallax2D moves itself to make up for part of the camera's movement: at scroll scale 0.5 it shifts along with the camera by half of the camera's move. On the main canvas, which the camera drags back by the full amount, that leaves it moving at half speed. A CanvasLayer is not dragged by the camera at all, so only Parallax2D's own shift is left, and it points the same way the camera went: +200 px in our test.
What to change
- Foreground layers, such as falling petals or leaves in front of the player at scroll scale above 1, should stay in front: use draw_behind() only on the background layers.
- Gaps at the edges while scrolling: Parallax2D background gaps.
- A HUD, health bar or menu does belong in a CanvasLayer: it is meant to stay still while the camera moves.
Backgrounds already set up this way: Asian Fantasy Parallax has 12 scenes of 7 Parallax2D layers each, from a still sky to a foreground layer at 1.25, in day, dusk and night. Each scene is a Node2D you drop straight into your level, with no CanvasLayer to switch off.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Asian Fantasy Parallax has 12 scenes of 7 Parallax2D layers each, in day, dusk and night, as Godot 4 scenes you drop straight into your level.
More how-tos: Skies and backgrounds
- Get the direction to the sun from a DirectionalLight3D, and fade sun effects out at sunset
- Day and night cycle: sun and sky together
- Night-only effects when the night crosses midnight
- Parallax2D background with no gaps
- Turn a time of day in hours into clock text that is always right
- A time skip that always goes forward