+2 votes

I've managed to get parallax scrolling working fine, but only if I set my sprite to be as big as or bigger than the screen itself. I tried originally with a tileable 256256 texture and it didn't work as intended at all (it just covered the top-left part of the screen). When I tiled it to 10241024 in a paint package (my screen is currently only 800*600) everything went well.

The reason I ask is that I intend to do several parallax layers and I'm wondering if I need to do this with each image.

in Engine by (75 points)

2 Answers

0 votes

I stumbled upon this as well, not sure if that's an actual issue but I had to account for various stuff like viewport size + custom transform caused by camera zoom etc, here's my function that tries to cover these cases in my project, hopefully useful:

func _update_size():

    for canvas_idx in get_child_count():
        # traversing multiple CanvasLayer/ParallaxBackground nodes...

        var canvas_layer = get_child(canvas_idx)
        if not canvas_layer is CanvasLayer:
            continue

        for idx in canvas_layer.get_child_count():
            # traversing multiple ParallaxLayer nodes...

            var layer_node = canvas_layer.get_child(idx)

            if not layer_node is ParallaxLayer:
                continue

            var canvas = layer_node.get_node('canvas')

            var tex_size = canvas.texture.get_size()
            var vp_size = viewport.size
            var vp_scale = viewport.canvas_transform.get_scale()

            var q = vp_size / tex_size
            q = q / vp_scale
            q = q.ceil()

            var x = q.x * tex_size.x
            var y = q.y * tex_size.y

            var new_size = Vector2(x, y)
            new_size *= 2 # just to be sure

            var mirroring = layer_node.motion_mirroring

            if mirroring.x > 0.0:
                mirroring.x = new_size.x

            if mirroring.y > 0.0:
                mirroring.y = new_size.y

            layer_node.motion_mirroring = mirroring

            var canvas_size
            if canvas is TextureRect:
                canvas_size = canvas.rect_size
            elif canvas is Sprite:
                canvas_size = canvas.region_rect.size

            if mirroring.x > 0.0:
                canvas_size.x = new_size.x

            if mirroring.y > 0.0:
                canvas_size.y = new_size.y

            if canvas is TextureRect:
                canvas.rect_size = canvas_size
            elif canvas is Sprite:
                canvas.region_rect.size = canvas_size
by (1,371 points)
0 votes

I know the question is old but I thought I'd leave the answer here for anyone else who has this problem.

For the parallax layer sprite, set region to enabled and the region rect w and h to multiples of the texture size.

For the parallax layer itself, set the x and y mirroring to the w and h values of the sprite region multiplied by the sprite scale if not 1,1.

parallax

by (26 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.