• 2D
  • Camera2D not working inside Parallax Background

I have some Parallax Layers inside a Parallax Background. One is my playfield, where my player is. I tried putting a camera2D node onto them, but when I run the scene, the viewport goes crazy. It jitters wildly every frame, jumping from the expected viewpoint to a position relative to the player down and to the right - the jump increases as the player start position shifts from the scene origin plus half the dimensions of the camera2D bounds.

Does anyone know how to fix this so that my Camera 2D will work? It seems to work just fun outside of the Parallax Background. Inside a Parallax Background, if not a child of a Parallax Layer, the camera offset simply lags behind where it should be, but it doesn't jitter wildly.

Why would the Camera be a child of the Parallax Background? That doesn't make any sense to me, maybe I am misunderstanding. In general, you want the camera on the highest part of the tree (under the root node), so that it is not affected by anything else except your own code.

The easiest thing would be to set the Camera position on process() (or physics_process()) to be offset from the player position some amount. You can also use interpolate or Tween to get smoother motion. But it depends on the type of game you are creating.

Sorry for the misunderstanding. The camera is a child of the player, not the parallax layers, with the intention being that the camera follows the player as they move and hit the drag margins. If I need to control everything in code, then I might as well be writing my own camera.

Sorry, I was a bit confused. For a side scroller you would want the Camera2D as a child of the player (or at least that is the easiest option). I tried it and it seems to be working for me. Can you take a screenshot of your tree? I think probably there is just something wrong with the hierarchy.

Not a problem. This stuff can be difficult to communicate. Here is what my scene tree looks like in the remote tab when running the test.

Some things I have noticed is that the selecting "follow the viewport," in ParallaxBackground helps ensure the camera does not lag behind the player. Enabling drag_margin_h, and drag_margin_v, stops the rapid jittering - but there are other strange behaviors, like the camera not starting centered where it should be.

Thank you for the help.

I've also made a minimal project reproducing the issue.

The player should be on the same level as the parallax background, not inside it.

Really? Hmmm. Well, that does work. It is strange to me that it would work this way though. Wouldn't one want for a player's motion to be tied to the parallax layer they're on? I could imagine a game where, for instance, one could go through a door in a foreground layer and come out on a background layer and the relative parallax values would work as expected.

But then, this works for everything I'm doing in my current project, so, yay! Thank you so much for the help!