Vary scrolling pattern if in Main Menu

Hi there!

Earlier on in the course, I wanted to achieve parallax scrolling only when the player is in motion

I went with:

extends ParallaxBackground

@onready var sprite = $ParallaxLayer/Sprite2D
@onready var scroll_speed = 20
@export var bg_texture: CompressedTexture2D = preload(“res://Assets/textures/bg/Blue.png”)

func _ready():
sprite.texture = bg_texture

func _process(delta):
var direction = Input.get_axis(“move_left”, “move_right”)
if direction > 0:
sprite.region_rect.position += delta * Vector2(scroll_speed, 0)
if direction < 0:
sprite.region_rect.position += delta * Vector2(-scroll_speed, 0)
if sprite.region_rect.position >= Vector2(64, 64):
sprite.region_rect.position = Vector2.ZERO

However, this poses an issue when instancing the BG for the main menu - there is no player input to capture.

How would one go about having the scroll pattern automate if we are on the main menu?

Thanks!

/Weeg

Hi Weeg,

Its been a while since i did the 2D course but i believe in the course we use a different method to create a parallax background.
The issue here is that the ParallaxBackground node and its layers move relative to the camera position so if there is no motion with no player that the camera is following then the parallax as you have found out wont work.

A better way to do this would be to implement the motion_scale property of the parallax.

onready var parallax_background := $ParallaxBackground

func _ready():
var layer1 = ParallaxLayer.new()
var layer2 = ParallaxLayer.new()
var layer3 = ParallaxLayer.new()

layer1.texture = load("res://path_to_layer1_texture.png")
layer2.texture = load("res://path_to_layer2_texture.png")
layer3.texture = load("res://path_to_layer3_texture.png")

layer1.motion_scale = Vector2(0.5, 0.5)
layer2.motion_scale = Vector2(0.8, 0.8)
layer3.motion_scale = Vector2(1.0, 1.0)

parallax_background.add_child(layer1)
parallax_background.add_child(layer2)
parallax_background.add_child(layer3)

Hope this helps

Hi Marc,

Thanks for your reply!

My implementation more or less mirrored the course - I just added some ‘if’ conditions to check if the player is moving. I was aiming to achieve something like we see in the original Sonic, where the bg only scrolls when he’s moving.

I tried out your code anyway but am thrown the error:

Invalid set index ‘texture’ (on base: ‘ParallaxLayer’) with value type ‘CompressedTexture2D’.

Not quite sure what that means.

My question is more around being able to execute a different type of scroll depending on which scene we’re instancing under. I appreciate it might be beyond the scope of the comments, but I’m not sure where to start looking.

Cheers!

/Gary

I think we cover this more in our up coming Godot Mobile course although its done on the Y axis as being a mobile course obviously you travel up the screen and it tracks the player position.
I’ve included a couple of screenshots below as a sneak preview of the up coming course that i think would help your game when it releases. Its a different method to what i was trying to get at before.

Hopefully this gets you to somewhere close to what you need as my initial explanation truthfully wasnt that great.

The error you are getting i have seen before but i cant for the life of me remember what the fix was

1 Like

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

Privacy & Terms