0 votes

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():
   set_process(true)
   pass

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
             pass
     pass

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!

in Engine by (116 points)

1 Answer

0 votes

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.

by (262 points)

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

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 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 webmaster@godotengine.org with your username.