0 votes

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?

in Engine by (12 points)

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?

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.

Which Area scene? Can you show your scene tree?

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

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.

1 Answer

0 votes

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.

by (24 points)
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.