Camera2D

    Camera node for 2D scenes.

    Camera node for 2D scenes. It forces the screen (current layer) to scroll following this node. This makes it easier (and faster) to program scrollable scenes than manually changing the position of -based nodes.

    This node is intended to be a simple helper to get things going quickly and it may happen that more functionality is desired to change how the camera works. To make your own custom camera node, inherit from Node2D and change the transform of the canvas by setting in Viewport (you can obtain the current by using Node.get_viewport).

    Note that the node’s position doesn’t represent the actual position of the screen, which may differ due to applied smoothing or limits. You can use to get the real position.

    Tutorials

    Methods

    void ( )
    voidclear_current ( )
    void ( )
    Vector2 ( ) const
    Vector2 ( ) const
    float ( Margin margin ) const
    get_limit ( margin ) const
    voidmake_current ( )
    void ( )
    voidset_drag_margin ( margin, float drag_margin )
    void ( Margin margin, limit )

    enum AnchorMode:

    • ANCHOR_MODE_FIXED_TOP_LEFT = 0 —- The camera’s position is fixed so that the top-left corner is always at the origin.
    • ANCHOR_MODE_DRAG_CENTER = 1 —- The camera’s position takes into account vertical/horizontal offsets and the screen size.

    enum Camera2DProcessMode:

    • CAMERA2D_PROCESS_PHYSICS = 0 —- The camera updates with the _physics_process callback.
    • CAMERA2D_PROCESS_IDLE = 1 —- The camera updates with the _process callback.

    Property Descriptions

    Default1
    Setterset_anchor_mode(value)
    Getterget_anchor_mode()

    The Camera2D’s anchor point. See constants.


    Defaultfalse
    Getteris_current()

    If true, the camera is the active camera for the current scene. Only one camera can be current, so setting a different camera current will disable this one.


    Setterset_custom_viewport(value)
    Getterget_custom_viewport()

    The custom node attached to the Camera2D. If null or not a Viewport, uses the default viewport instead.


    • drag_margin_bottom
    Default0.2
    Setterset_drag_margin(value)
    Getterget_drag_margin()

    Bottom margin needed to drag the camera. A value of 1 makes the camera move only when reaching the edge of the screen.


    • bool drag_margin_h_enabled
    Defaultfalse
    Setterset_h_drag_enabled(value)
    Getteris_h_drag_enabled()

    If true, the camera only moves when reaching the horizontal drag margins. If false, the camera moves horizontally regardless of margins.


    • drag_margin_left
    Default0.2
    Setterset_drag_margin(value)
    Getterget_drag_margin()

    Left margin needed to drag the camera. A value of 1 makes the camera move only when reaching the edge of the screen.


    Default0.2
    Setterset_drag_margin(value)
    Getterget_drag_margin()

    Right margin needed to drag the camera. A value of 1 makes the camera move only when reaching the edge of the screen.


    • drag_margin_top

    Top margin needed to drag the camera. A value of 1 makes the camera move only when reaching the edge of the screen.


    • bool drag_margin_v_enabled
    Defaultfalse
    Setterset_v_drag_enabled(value)
    Getteris_v_drag_enabled()

    • editor_draw_drag_margin
    Defaultfalse
    Setterset_margin_drawing_enabled(value)
    Getteris_margin_drawing_enabled()

    If true, draws the camera’s drag margin rectangle in the editor.


    • bool editor_draw_limits
    Default
    Setterset_limit_drawing_enabled(value)
    Getteris_limit_drawing_enabled()

    If true, draws the camera’s limits rectangle in the editor.


    • editor_draw_screen
    Defaulttrue
    Setterset_screen_drawing_enabled(value)
    Getteris_screen_drawing_enabled()

    If true, draws the camera’s screen rectangle in the editor.


    • int limit_bottom
    Default10000000
    Setterset_limit(value)
    Getterget_limit()

    Bottom scroll limit in pixels. The camera stops moving when reaching this value.


    • limit_left
    Default-10000000
    Setterset_limit(value)
    Getterget_limit()

    Left scroll limit in pixels. The camera stops moving when reaching this value.


    • int limit_right
    Default10000000
    Setterset_limit(value)
    Getterget_limit()

    Right scroll limit in pixels. The camera stops moving when reaching this value.


    • limit_smoothed
    Defaultfalse
    Setterset_limit_smoothing_enabled(value)
    Getteris_limit_smoothing_enabled()

    If true, the camera smoothly stops when reaches its limits.


    Top scroll limit in pixels. The camera stops moving when reaching this value.


    • offset
    DefaultVector2( 0, 0 )
    Setterset_offset(value)
    Getterget_offset()

    The camera’s offset, useful for looking around or camera shake animations.


    Default0.0
    Setterset_h_offset(value)
    Getterget_h_offset()

    The horizontal offset of the camera, relative to the drag margins.

    Note: Offset H is used only to force offset relative to margins. It’s not updated in any way if drag margins are enabled and can be used to set initial offset.


    Default0.0
    Setterset_v_offset(value)
    Getterget_v_offset()

    The vertical offset of the camera, relative to the drag margins.

    Note: Used the same as .


    Default1
    Setterset_process_mode(value)
    Getterget_process_mode()

    The camera’s process callback. See .


    Defaultfalse
    Setterset_rotating(value)
    Getteris_rotating()

    • smoothing_enabled
    Defaultfalse
    Setterset_enable_follow_smoothing(value)
    Getteris_follow_smoothing_enabled()

    If true, the camera smoothly moves towards the target at smoothing_speed.


    • smoothing_speed
    Default5.0
    Setterset_follow_smoothing(value)
    Getterget_follow_smoothing()

    Speed in pixels per second of the camera’s smoothing effect when smoothing_enabled is true.


    • zoom
    DefaultVector2( 1, 1 )
    Setterset_zoom(value)
    Getterget_zoom()

    The camera’s zoom relative to the viewport. Values larger than Vector2(1, 1) zoom out and smaller values zoom in. For an example, use Vector2(0.5, 0.5) for a 2× zoom-in, and Vector2(4, 4) for a 4× zoom-out.

    • void align ( )

    Aligns the camera to the tracked node.


    • void clear_current ( )

    Removes any Camera2D from the ancestor Viewport‘s internal currently-assigned camera.


    • void force_update_scroll ( )

    Forces the camera to update scroll immediately.


    • get_camera_position ( ) const

    Returns the camera position.


    • Vector2 get_camera_screen_center ( ) const

    Returns the location of the ‘s screen-center, relative to the origin.


    • get_drag_margin ( Margin margin ) const

    Returns the specified margin. See also , drag_margin_top, , and drag_margin_right.


    • get_limit ( Margin margin ) const

    Returns the specified camera limit. See also , limit_top, , and limit_right.


    • void make_current ( )

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


    • void reset_smoothing ( )

    Sets the camera’s position immediately to its current smoothing destination.

    This has no effect if smoothing is disabled.


    • void set_drag_margin ( margin, float drag_margin )

    Sets the specified margin. See also , drag_margin_top, , and drag_margin_right.


    • void set_limit ( margin, int limit )