All Questions

Community

B
Jaden Barrio

Camera

Whenever my character dies the camera teleports to a certain place on the level. Is there a way to keep the camera where the character dies?

  • Nathan Lovato replied

    This happens because the player's camera node gets deleted (freed) along with the player instance. The camera isn't available anymore, so the engine falls back to the default 2D camera, which, I believe, is centered on the scene's origin.

    You can avoid that in several ways, like reparenting the camera before deleting the player. Alternatively, you could not delete the player but rather hide and deactivate the node's collisions, which means it stays in the scene tree and the camera stays active.

    A third solution, the one I'd use and recommend, would be to have the camera separate from the Player in the first place, and to use a RemoteTransform2D node to synchronize its position with the player. The node setup looks like this.

    For this to work, you need to set the RemoteTransform2D's Remote Path property to target the Camera2D node.

    The main reason we didn't show this in the course is that you likely want a different way of creating and setting up levels to avoid having to duplicate the nodes in every scene, which was beyond the scope of this course.