Parallax Background, Player, and Camera

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

I’m trying to make a scene where the player is walking in a field. The player will be moving with the camera, and thanks to that, the field and the sky will be moving; with parallax background the field will be moving faster than the sky or vice versa, I am trying to make the player be able to move faster/slower than the camera but if the player tries to leave the camera from the left, right, or bottom, the player wouldn’t bypass it, but if the player tries to leave the grass, the same thing I tried implementing this in such:

  • Node2D
    • Player
    • ParallaxBackground
      • ParallaxLayer
        • field
        • sky
    • Camera (current=true)

then in the Node2D script:

# I have already done the player input code in the player scene

extends Node2D

onready var player   = get_node("player")
onready var camera = get_node("camera")

func _ready():

func _process(delta):
     camera.position.x += 5
     player.position.x += 5

     if(player.position.x > camera.position.x): # player shouldn't leave from the right

         player.position.x = camera.position.x
     if(player.position.x < camera.position.x - camera.position.x): # player shouldn't leave from the left

         player.position.x = camera.position.x - camera.position.x

      if(player.position.y > camera.position.y): # player shouldn't leave from the bottom
           player.position.y = camera.position.y

      if(player.position.y < WhateverTheHighestPointOfTheGrassIs.y): # player shouldn't leave the grass

            player.position.y = WhateverTheHighestPointOfTheGrassIs.y

Everything is just messed up and the background doesn’t move plus the fact that the camera doesn’t even show the background
Help is much appreciated!

:bust_in_silhouette: Reply From: Artium Nihamkin

You are not scrolling the parallax background at all, just moving the camera, and at some point it will be looking too much to the left, so it will not see bg at all.

This happens every frame, and +5 might be too fast and after few seconds you see a gray screen without the bg.

Also, instead of camera.position.x += 5 do camera.position.x += SOME_AMOUNT * delta.

I suggest trying to implement first parallax without camera. Simply scroll it right or left when the player is near the margins.

One more thing, make sure that “Ignore Camera Zoom” of the ParallaxBackground is unchecked this can resolve some issues you might encounter.

ah I see, I’ll try that, thanks!

Dava | 2018-02-14 03:38