Parallax Layer (scale != 1.0) displaces every time Camera2D zoom changes

Godot Version

Godot 4.2.1

Question

I’ve made a 2D scene with a parallax background. Parallax Layer’s scale is set to Vector2(0.9, 0.9). CharacterBody2D (player node) has a child node of Camera2D, which is active in this scene.
Camera2D’s zoom is manipulated by a script (GDScript) that makes zoom change when Shift key is held/not held. Camera is centered on player node.
Each time zoom of camera changes, i see that Parallax Background displaces from its original position relatively to player node. Like, every time camera’s zoom is lowered from Vector2(1.0, 1.0) to Vector2(0.6, 0.6) background moves a few pixels to top-left relatively to player node.
If Parallax Layer’s scale is set to Vector2(1.0, 1.0) or “Ignore Camera Zoom” is enabled, background displacement doesn’t seem to be a problem, but I need to dynamicly change camera’s zoom and I don’t want parallax elements to move every time i zoom out.
2024-03-1513-48-34-ezgif.com-optimize
How can I fix this problem?

Nevermind, found solution for myself.

for c in get_node('../ParallaxBackground').get_children():
		c.motion_offset = get_viewport_rect().size*0.5*pow(1-c.motion_scale.x,1)*(1.0 + 1.0/$Camera2D.zoom.x)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.