Camera2D

Inherits: Node2D < CanvasItem < Node < Object

Category: Core

Brief Description

Camera node for 2D scenes.

Methods

void align ( )
void clear_current ( )
void force_update_scroll ( )
Vector2 get_camera_position ( ) const
Vector2 get_camera_screen_center ( ) const
void make_current ( )
void reset_smoothing ( )

Enumerations

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.

Description

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 CanvasItem based nodes.

This node is intended to be a simple helper to get things going quickly and it may happen often that more functionality is desired to change how the camera works. To make your own custom camera node, simply inherit from Node2D and change the transform of the canvas by calling get_viewport().set_canvas_transform(m) in Viewport.

Property Descriptions

Setter set_anchor_mode(value)
Getter get_anchor_mode()

The Camera2D’s anchor point. See ANCHOR_MODE_* constants.

Getter is_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.

  • Node custom_viewport
Setter set_custom_viewport(value)
Getter get_custom_viewport()

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

  • float drag_margin_bottom
Setter set_drag_margin(value)
Getter get_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
Setter set_h_drag_enabled(value)
Getter is_h_drag_enabled()

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

Setter set_drag_margin(value)
Getter get_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.

  • float drag_margin_right
Setter set_drag_margin(value)
Getter get_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.

Setter set_drag_margin(value)
Getter get_drag_margin()

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
Setter set_v_drag_enabled(value)
Getter is_v_drag_enabled()

If true the camera only moves when reaching the vertical drag margins. If false the camera moves vertically regardless of margins. Default value: true.

  • bool editor_draw_drag_margin
Setter set_margin_drawing_enabled(value)
Getter is_margin_drawing_enabled()

If true draws the camera’s drag margin rectangle in the editor. Default value: false

  • bool editor_draw_limits
Setter set_limit_drawing_enabled(value)
Getter is_limit_drawing_enabled()

If true draws the camera’s limits rectangle in the editor. Default value: true

  • bool editor_draw_screen
Setter set_screen_drawing_enabled(value)
Getter is_screen_drawing_enabled()

If true draws the camera’s screen rectangle in the editor. Default value: false

  • int limit_bottom
Setter set_limit(value)
Getter get_limit()

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

  • int limit_left
Setter set_limit(value)
Getter get_limit()

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

  • int limit_right
Setter set_limit(value)
Getter get_limit()

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

  • bool limit_smoothed
Setter set_limit_smoothing_enabled(value)
Getter is_limit_smoothing_enabled()

If true the camera smoothly stops when reaches its limits. Default value: false

  • int limit_top
Setter set_limit(value)
Getter get_limit()

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

Setter set_offset(value)
Getter get_offset()

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

Setter set_h_offset(value)
Getter get_h_offset()

The horizontal offset of the camera, relative to the drag margins. Default value: 0

Setter set_v_offset(value)
Getter get_v_offset()

The vertical offset of the camera, relative to the drag margins. Default value: 0

Setter set_rotating(value)
Getter is_rotating()

If true the camera rotates with the target. Default value: false

  • bool smoothing_enabled
Setter set_enable_follow_smoothing(value)
Getter is_follow_smoothing_enabled()

If true the camera smoothly moves towards the target at smoothing_speed. Default value: false

Setter set_follow_smoothing(value)
Getter get_follow_smoothing()

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

Setter set_zoom(value)
Getter get_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 2x zoom in, and Vector2(4, 4) for a 4x zoom out.

Method Descriptions

  • void align ( )

Align 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 ( )

Force the camera to update scroll immediately.

  • Vector2 get_camera_position ( ) const

Return the camera position.

  • Vector2 get_camera_screen_center ( ) const

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

  • void make_current ( )

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

  • void reset_smoothing ( )

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

This has no effect if smoothing is disabled.