Infinite Scrolling ParallaxBackgound with Camera2d

Godot Version

4.2 Stable


So, I am new to godot and need to move a parallax background where there is a player with a camera 2d but the problem is the _process is constant when the camera2d is spawned so the background doesn’t move. So pretty much I want to move the background even if the player isn’t moving. I tried this but it didn’t really work.

extends ParallaxBackground

var scrolling_speed = 1000
var lastDelta: float = 0
var actualDelta: float = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if lastDelta == delta:
		actualDelta = lastDelta + 0.01
		actualDelta = delta
	lastDelta = actualDelta
	print(str(actualDelta) + " " + str(lastDelta) + " " + str(actualDelta))
	scroll_offset.x -= scrolling_speed * delta
1 Like

The scroll_offset is

Calculated automatically when using a Camera2D, but can be used to manually manage scrolling when no camera is present.

So you can’t set it manually if your viewport has a Camera2D attached as a child. (Technically, your main scene is the child of the main viewport, so you do)

So I would have to use something like a gif to get it to work then?

You can use a shader to scroll a texture on a sprite
Here’s a good one

2 things i noticed in your code:

Your print has actualDelta as the first and last param, one of these should be delta. It’s not an issue as such, but could be causing you confusion.

Your scroll_speed is an integer, which means scroll_offset calculation is integer math and is probably not what your expecting. Eg 1000*0.1 =0 rather than 100.