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_camera_screen_center to get the real position.
Tutorials¶
Properties¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Methods¶
void |
align ( ) |
void |
|
get_camera_position ( ) const |
|
get_camera_screen_center ( ) const |
|
get_drag_margin ( Side margin ) const |
|
void |
reset_smoothing ( ) |
void |
set_drag_margin ( Side margin, float drag_margin ) |
void |
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.
enum Camera2DProcessCallback:
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¶
AnchorMode anchor_mode
Default |
|
Setter |
set_anchor_mode(value) |
Getter |
get_anchor_mode() |
The Camera2D's anchor point. See AnchorMode constants.
bool current
Default |
|
Setter |
set_current(value) |
Getter |
is_current() |
If true
, the camera acts as the active camera for its Viewport ancestor. Only one camera can be current in a given viewport, so setting a different camera in the same viewport current
will disable whatever camera was already active in that viewport.
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_bottom_margin
Default |
|
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 bottom edge of the screen.
bool drag_horizontal_enabled
Default |
|
Setter |
set_drag_horizontal_enabled(value) |
Getter |
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
Default |
|
Setter |
set_drag_horizontal_offset(value) |
Getter |
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
Default |
|
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 left edge of the screen.
float drag_right_margin
Default |
|
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 right edge of the screen.
float drag_top_margin
Default |
|
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 top edge of the screen.
bool drag_vertical_enabled
Default |
|
Setter |
set_drag_vertical_enabled(value) |
Getter |
is_drag_vertical_enabled() |
If true
, the camera only moves when reaching the vertical (top and bottom) drag margins. If false
, the camera moves vertically regardless of the drag margins.
float drag_vertical_offset
Default |
|
Setter |
set_drag_vertical_offset(value) |
Getter |
get_drag_vertical_offset() |
The relative vertical drag offset of the camera between the bottom (-1
) and top (1
) drag margins.
Note: Used to set the initial vertical drag offset; determine the current offset; or force the current offset. It's not automatically updated when drag_vertical_enabled is true
or the drag margins are changed.
bool editor_draw_drag_margin
Default |
|
Setter |
set_margin_drawing_enabled(value) |
Getter |
is_margin_drawing_enabled() |
If true
, draws the camera's drag margin rectangle in the editor.
bool editor_draw_limits
Default |
|
Setter |
set_limit_drawing_enabled(value) |
Getter |
is_limit_drawing_enabled() |
If true
, draws the camera's limits rectangle in the editor.
bool editor_draw_screen
Default |
|
Setter |
set_screen_drawing_enabled(value) |
Getter |
is_screen_drawing_enabled() |
If true
, draws the camera's screen rectangle in the editor.
int limit_bottom
Default |
|
Setter |
set_limit(value) |
Getter |
get_limit() |
Bottom scroll limit in pixels. The camera stops moving when reaching this value, but offset can push the view past the limit.
int limit_left
Default |
|
Setter |
set_limit(value) |
Getter |
get_limit() |
Left scroll limit in pixels. The camera stops moving when reaching this value, but offset can push the view past the limit.
int limit_right
Default |
|
Setter |
set_limit(value) |
Getter |
get_limit() |
Right scroll limit in pixels. The camera stops moving when reaching this value, but offset can push the view past the limit.
bool limit_smoothed
Default |
|
Setter |
set_limit_smoothing_enabled(value) |
Getter |
is_limit_smoothing_enabled() |
If true
, the camera smoothly stops when reaches its limits.
This property has no effect if smoothing_enabled is false
.
Note: To immediately update the camera's position to be within limits without smoothing, even with this setting enabled, invoke reset_smoothing.
int limit_top
Default |
|
Setter |
set_limit(value) |
Getter |
get_limit() |
Top scroll limit in pixels. The camera stops moving when reaching this value, but offset can push the view past the limit.
Vector2 offset
Default |
|
Setter |
set_offset(value) |
Getter |
get_offset() |
The camera's relative offset. Useful for looking around or camera shake animations. The offsetted camera can go past the limits defined in limit_top, limit_bottom, limit_left and limit_right.
Camera2DProcessCallback process_callback
Default |
|
Setter |
set_process_callback(value) |
Getter |
get_process_callback() |
The camera's process callback. See Camera2DProcessCallback.
bool rotating
Default |
|
Setter |
set_rotating(value) |
Getter |
is_rotating() |
If true
, the camera view rotates with the target.
bool smoothing_enabled
Default |
|
Setter |
set_enable_follow_smoothing(value) |
Getter |
is_follow_smoothing_enabled() |
If true
, the camera smoothly moves towards the target at smoothing_speed.
float smoothing_speed
Default |
|
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
.
Vector2 zoom
Default |
|
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 2× zoom-in, and Vector2(4, 4)
for a 4× zoom-out.
Method Descriptions¶
void align ( )
Aligns the camera to the tracked node.
void force_update_scroll ( )
Forces the camera to update scroll immediately.
Vector2 get_camera_position ( ) const
Returns the camera's position
(the tracked point the camera attempts to follow), relative to the origin.
Note: The returned value is not the same as Node2D.position or Node2D.global_position, as it is affected by the drag
properties.
Vector2 get_camera_screen_center ( ) const
Returns the location of the Camera2D
's screen-center, relative to the origin.
Note: The real position
of the camera may be different, see get_camera_position.
Returns the specified Side's margin. See also drag_bottom_margin, drag_top_margin, drag_left_margin, and drag_right_margin.
Returns the camera limit for the specified Side. See also limit_bottom, limit_top, limit_left, and limit_right.
void reset_smoothing ( )
Sets the camera's position immediately to its current smoothing destination.
This method has no effect if smoothing_enabled is false
.
Sets the specified Side's margin. See also drag_bottom_margin, drag_top_margin, drag_left_margin, and drag_right_margin.
Sets the camera limit for the specified Side. See also limit_bottom, limit_top, limit_left, and limit_right.