← Pixel Dreamer Studio

Fix a Parallax2D background that slides the wrong way, or stays still, in a CanvasLayer

Parallax2D already moves itself against the camera. Inside a CanvasLayer that does not follow the camera, that move is all you see, and it goes the wrong way.

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.

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

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