How to display Parallax Backgroung between normal sprites?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By aleferna

I just want to know if there is a way to display the parallax between other sprites. My game is basically a fish that eats flies. As such, I need the fish to be displayed behind the parallax so that it looks like its underwater, while the flies are above it. I’ve tried adding the player to a CanvasLayer and setting its “layer” property to -2, but it causes a weird bug where the Camera2D attached to its center moves along but can’t keep up with the fish speed, which wasn’t a problem before they were both added as children of the CanvasLayer. I then tried making a custom camera, which was updated each frame by:

func update_camera():
var canvas_transform = get_viewport().get_canvas_transform()
canvas_transform2 = -get_parent().position + Globals.screen_size/2

Very simple stuff. Still the camera starts falling behind the fish, which makes me think the transformations must be also applied to the CanvasLayer or something…

Please help.

Bug explanation, CustomCamera code and Node tree:

Code and tree

:bust_in_silhouette: Reply From: lofi

i think easiest solution is to move down your fish or move up your parallax in the tree

What you mean? If you mean taking fish out of CanvasLayer and ordering it bellow the parallax, this wont work because layers are rendered always before or after sprites. Thanks for the suggestion though.

aleferna | 2019-11-12 21:41

:bust_in_silhouette: Reply From: lofi

try this, get rid off canvas layer then follow the next tree structure


main is gonna be parent scene, parallaxbackground is gonna be child of main and fish, parallax layer and the flies are gonna be children of parallaxbackground

This has worsened the issue, now fish and fly move in the same velocity in relation to the background (meaning that fish cant get close to fly) and the camera is still bugged and cant keep up… I also tried adding fish and fly as children of parallaxlayer, respectively before and after sprite, but that didnt work either.

aleferna | 2019-11-13 14:35