Godot Version
4.0
Question
I made a parallax backround that moves when the player moves. However after i made it i realised i would much rather the movement of the layers to always play like an animation regardless of the player moving or not. Ideally i would like it to move slower when the player is standing still and a lot faster when the player is actually moving, i think that will happen anyway tho if i can get the parallax layers to always be moving without the camera moving.
Is there a way to do this ?
Yeah there is. Currently in my game I have a parallax layer that is constantly scrolling. All you would have to do is add or minus by a certain amount. Using a global variable you can make it so when a person is standing still it minus the amount of scrolling but when a person is moving it increases. Let me show you. Here is my player code.
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var coyotetime = 0
var coyote = 0.25
var holdtojump = 0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim = get_node("AnimationPlayer")
func ready():
get_node("AnimatedSprite2D").play("Idle")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
coyotetime -= delta
if is_on_floor():
coyotetime = coyote
# Handle jump.
if Input.is_action_just_pressed("up") and Global.Jupiter == false:
if is_on_floor() or coyotetime >= 0:
coyotetime = 0
holdtojump = 0
velocity.y = JUMP_VELOCITY
anim.play("Jump")
if Global.settingholdtojump == true:
if Input.is_action_just_released("up") and holdtojump == 0 and velocity.y:
velocity.y = 1
holdtojump = 1
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction == -1:
get_node("AnimatedSprite2D").flip_h = true
elif direction == 1:
get_node("AnimatedSprite2D").flip_h = false
if direction:
velocity.x = direction * SPEED
if velocity.y == 0:
anim.play("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0:
anim.play("Idle")
if velocity.y > 0:
anim.play("Fall")
move_and_slide()
Next add the global variables. This can be done in this video https://www.youtube.com/watch?v=sc-tEPdLZhk&list=PLVtzemUFo9_gmOXtgXdRSJLuHxzc813EO&index=7&t=14s
Name it something like Scrolling speed. Then use this code instead.
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var coyotetime = 0
var coyote = 0.25
var holdtojump = 0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim = get_node("AnimationPlayer")
func ready():
get_node("AnimatedSprite2D").play("Idle")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
coyotetime -= delta
if is_on_floor():
coyotetime = coyote
# Handle jump.
if Input.is_action_just_pressed("up") and Global.Jupiter == false:
Global.ScrollingSpeed = 100
if is_on_floor() or coyotetime >= 0:
coyotetime = 0
holdtojump = 0
velocity.y = JUMP_VELOCITY
anim.play("Jump")
if Global.settingholdtojump == true:
if Input.is_action_just_released("up") and holdtojump == 0 and velocity.y:
velocity.y = 1
holdtojump = 1
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction == -1:
get_node("AnimatedSprite2D").flip_h = true
elif direction == 1:
get_node("AnimatedSprite2D").flip_h = false
if direction:
velocity.x = direction * SPEED
if velocity.y == 0:
Global.ScrollingSpeed = 100
anim.play("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0:
Global.ScrollingSpeed = 60
anim.play("Idle")
if velocity.y > 0:
Global.ScrollingSpeed = 100
anim.play("Fall")
move_and_slide()
Basically if you are moving just set the scrolling speed higher and if your not make it lower. Next go to your parallax background, add a script, and then type the following
func _process(delta):
scroll_offset.x -= Global.ScrollingSpeed * delta
1 Like
This in theory should work but I haven’t tested it out my self
1 Like
You might be able to pick out useful bits from an old tutorial I wrote:
Vertical Scrolling Parallax Backgrounds in Godot
It was written for Godot 3, but it might still be useful. There’s a link to the GitHub there with the code as well.
1 Like