Skip to content

Instantly share code, notes, and snippets.

@timoschwarzer
Last active May 17, 2023 06:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timoschwarzer/c7f6c8185a0c82c294a4 to your computer and use it in GitHub Desktop.
Save timoschwarzer/c7f6c8185a0c82c294a4 to your computer and use it in GitHub Desktop.
Parallax implementation which works with camera zoom
# Setup:
# - Node2D
# - ParallaxLayer (For motion_scale)
# - Sprite
# - Sprite
# - ...
# - ParallaxLayer
# - Sprite
# - ...
# - ...
extends Node2D
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
var scale = get_viewport().get_canvas_transform().get_scale()
var rect = get_viewport_rect()
var cam = get_node("/root/Stage/Character/Player/MainCamera")
var transform = cam.get_camera_screen_center() + rect.size / scale / 2 - rect.size / 2
for p_node in get_children():
var transform_pos = -transform * (p_node.get_motion_scale() - Vector2(1, 1))
p_node.set_pos(transform_pos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment