Search code examples
godotgdscript

How to exactly position a Light2D when influenced by Camera2d with smoothing on


First, I'm a beginner in Godot and Game dev. In Godot I'm using a Light2D following the player acting as a kind of main light. The root of the scene "player" is a KinematicBody2d and the Light2D and Camera2d are child nodes. For the camera smoothing is enabled. As explained within the Godot documentation, I can use get_camera_screen_center() to get the real position of the camera. I thought something as simple as light.position = $"Camera".get_camera_position() would position the light texture right, but now it's in the bottom/right corner. What do I miss? How to calculate the offset to use? I tried half of the view port size but this is still incorrect.

I would most like to have a signal emitted from the Light2D (with the real position) every time the real (smoothed) position is changing. But this seems not to be not implemented.


Solution

  • The method get_camera_position will give you a global position, not a position relative to the parent node.

    In consequence, you need to set the light global position, instead of the light position. That is:

    $Light2D.global_position = $Camera2D.get_camera_screen_center()