How to make the camera2d of a child scenes the current camera?

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

I have a Player scene that handles the player input and movements and it has a Camera2d node as a child of this player. I have a scene for a game area that will procedurally generate some things into 3 canvas layers. I have a SubLayer, PlayerLayer, and SuperLayer. I have an instance of my player sceen as a child of the PlayerLayer. The PlayerLayer also contains a few randomly spawned rocks that the player can collide with.

The camera, however, is not moving with the Player, I believe because the camera is not a direct child of the root. If I move the player outside this layer, the camera will stay centered on the player who appears to move as the player will still rotate with inputs. But the rocks that have been rendered keep their position relative to the player.

How do I make the player’s camera the current camera when it is within the PlayerLayer canvas?

It kind of sounds like you are updating the position of the rocks along with your Player. The rocks aren’t being added as children of the player are they?

Eric Ellingson | 2019-08-23 05:18

No, the rocks are being added as a child of PlayerLayer.
The Player is it’s own scene, and SmallRock is it’s own scene that gets instances by the code in the Area scene.

McGill25 | 2019-08-23 05:52

Which Area scene? Can you show your scene tree?

Eric Ellingson | 2019-08-23 13:34

I tried adding a picture of my screen in my comment but it didn’t seem to work properly. My Area tree is setup as follows:

Area (2DNode)

  • SubLayer (CanvasLayer)
  • PlayerLayer (CanvasLayer)
    …(SmallRock)'s instanced by Area code.
  • SuperLayer (CanvasLayer)

My Player scene:

Player (KinematicBody2D)

  • Camera2D
  • AnimatedSprite
  • CollisionShape2D

McGill25 | 2019-08-23 15:42

I’ve been doing some more research today and I think I am going to opt for hard coded physics layers for different object types instead. It seems that will be cleaner and far easier to work with.

McGill25 | 2019-08-24 00:09

:bust_in_silhouette: Reply From: jhigginbotham64

The strict answer to your question, about making the camera “current”, is Camera2D.make_current(). Alternatively you can check the “Current” box in the Inspector for the player’s Camera2D. If you didn’t do either of those at any point, or if the current property is somehow otherwise set to false, the camera will not follow the player.

Your question mentioned specifically “when it is within the PlayerLayer canvas”. So far as I can tell the answer is the same. From the docs (emphasis added):

Make this the current 2D camera for the scene (viewport and layer), in case there’s many cameras in the scene.