Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Camera2D

Inherits: Node2D < CanvasItem < Node < Object

Camera node for 2D scenes.

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.

Cameras register themselves in the nearest Viewport node (when ascending the tree). Only one camera can be active per viewport. If no viewport is available ascending the tree, the camera will register in the global viewport.

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

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

Tutorials

Properties

AnchorMode

anchor_mode

1

Node

custom_viewport

float

drag_bottom_margin

0.2

bool

drag_horizontal_enabled

false

float

drag_horizontal_offset

0.0

float

drag_left_margin

0.2

float

drag_right_margin

0.2

float

drag_top_margin

0.2

bool

drag_vertical_enabled

false

float

drag_vertical_offset

0.0

bool

editor_draw_drag_margin

false

bool

editor_draw_limits

false

bool

editor_draw_screen

true

bool

enabled

true

bool

ignore_rotation

true

int

limit_bottom

10000000

int

limit_left

-10000000

int

limit_right

10000000

bool

limit_smoothed

false

int

limit_top

-10000000

Vector2

offset

Vector2(0, 0)

bool

position_smoothing_enabled

false

float

position_smoothing_speed

5.0

Camera2DProcessCallback

process_callback

1

bool

rotation_smoothing_enabled

false

float

rotation_smoothing_speed

5.0

Vector2

zoom

Vector2(1, 1)

Methods

void

align ( )

void

force_update_scroll ( )

float

get_drag_margin ( Side margin ) const

int

get_limit ( Side margin ) const

Vector2

get_screen_center_position ( ) const

Vector2

get_target_position ( ) const

bool

is_current ( ) const

void

make_current ( )

void

reset_smoothing ( )

void

set_drag_margin ( Side margin, float drag_margin )

void

set_limit ( Side margin, int limit )


Enumerations

enum AnchorMode:

AnchorMode ANCHOR_MODE_FIXED_TOP_LEFT = 0

The camera's position is fixed so that the top-left corner is always at the origin.

AnchorMode ANCHOR_MODE_DRAG_CENTER = 1

The camera's position takes into account vertical/horizontal offsets and the screen size.


enum Camera2DProcessCallback:

Camera2DProcessCallback CAMERA2D_PROCESS_PHYSICS = 0

The camera updates during physics frames (see Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS).

Camera2DProcessCallback CAMERA2D_PROCESS_IDLE = 1

The camera updates during process frames (see Node.NOTIFICATION_INTERNAL_PROCESS).


Property Descriptions

AnchorMode anchor_mode = 1

The Camera2D's anchor point. See AnchorMode constants.


Node custom_viewport

  • void set_custom_viewport ( Node value )

  • Node get_custom_viewport ( )

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


float drag_bottom_margin = 0.2

  • void set_drag_margin ( Side margin, float drag_margin )

  • float get_drag_margin ( Side margin ) const

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


bool drag_horizontal_enabled = false

  • void set_drag_horizontal_enabled ( bool value )

  • bool is_drag_horizontal_enabled ( )

If true, the camera only moves when reaching the horizontal (left and right) drag margins. If false, the camera moves horizontally regardless of margins.


float drag_horizontal_offset = 0.0

  • void set_drag_horizontal_offset ( float value )

  • float get_drag_horizontal_offset ( )

The relative horizontal drag offset of the camera between the right (-1) and left (1) drag margins.

Note: Used to set the initial horizontal drag offset; determine the current offset; or force the current offset. It's not automatically updated when drag_horizontal_enabled is true or the drag margins are changed.


float drag_left_margin = 0.2

  • void set_drag_margin ( Side margin, float drag_margin )

  • float get_drag_margin ( Side margin ) const

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


float drag_right_margin = 0.2

  • void set_drag_margin ( Side margin, float drag_margin )

  • float get_drag_margin ( Side margin ) const

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


float drag_top_margin = 0.2

  • void set_drag_margin ( Side margin, float drag_margin )

  • float get_drag_margin ( Side margin ) const

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


bool drag_vertical_enabled = false<