Class Camera2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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 CanvasTransform in Viewport (you can obtain the current Viewport by using GetViewport()).
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 GetScreenCenterPosition() to get the real position.
public class Camera2D : Node2D, IDisposable
- Inheritance
-
Camera2D
- Implements
- Inherited Members
Constructors
Camera2D()
public Camera2D()
Properties
AnchorMode
The Camera2D's anchor point. See Camera2D.AnchorModeEnum constants.
public Camera2D.AnchorModeEnum AnchorMode { get; set; }
Property Value
CustomViewport
The custom Viewport node attached to the Camera2D. If null
or not a Viewport, uses the default viewport instead.
public Node CustomViewport { get; set; }
Property Value
DragBottomMargin
Bottom margin needed to drag the camera. A value of 1
makes the camera move only when reaching the bottom edge of the screen.
public float DragBottomMargin { get; set; }
Property Value
DragHorizontalEnabled
If true
, the camera only moves when reaching the horizontal (left and right) drag margins. If false
, the camera moves horizontally regardless of margins.
public bool DragHorizontalEnabled { get; set; }
Property Value
DragHorizontalOffset
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 DragHorizontalEnabled is true
or the drag margins are changed.
public float DragHorizontalOffset { get; set; }
Property Value
DragLeftMargin
Left margin needed to drag the camera. A value of 1
makes the camera move only when reaching the left edge of the screen.
public float DragLeftMargin { get; set; }
Property Value
DragRightMargin
Right margin needed to drag the camera. A value of 1
makes the camera move only when reaching the right edge of the screen.
public float DragRightMargin { get; set; }
Property Value
DragTopMargin
Top margin needed to drag the camera. A value of 1
makes the camera move only when reaching the top edge of the screen.
public float DragTopMargin { get; set; }
Property Value
DragVerticalEnabled
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.
public bool DragVerticalEnabled { get; set; }
Property Value
DragVerticalOffset
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 DragVerticalEnabled is true
or the drag margins are changed.
public float DragVerticalOffset { get; set; }
Property Value
EditorDrawDragMargin
If true
, draws the camera's drag margin rectangle in the editor.
public bool EditorDrawDragMargin { get; set; }
Property Value
EditorDrawLimits
If true
, draws the camera's limits rectangle in the editor.
public bool EditorDrawLimits { get; set; }
Property Value
EditorDrawScreen
If true
, draws the camera's screen rectangle in the editor.
public bool EditorDrawScreen { get; set; }
Property Value
Enabled
Controls whether the camera can be active or not. If true
, the Camera2D will become the main camera when it enters the scene tree and there is no active camera currently (see GetCamera2D()).
When the camera is currently active and Enabled is set to false
, the next enabled Camera2D in the scene tree will become active.
public bool Enabled { get; set; }
Property Value
IgnoreRotation
If true
, the camera's rendered view is not affected by its Rotation and GlobalRotation.
public bool IgnoreRotation { get; set; }
Property Value
LimitBottom
Bottom scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.
public int LimitBottom { get; set; }
Property Value
LimitLeft
Left scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.
public int LimitLeft { get; set; }
Property Value
LimitRight
Right scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.
public int LimitRight { get; set; }
Property Value
LimitSmoothed
If true
, the camera smoothly stops when reaches its limits.
This property has no effect if PositionSmoothingEnabled is false
.
Note: To immediately update the camera's position to be within limits without smoothing, even with this setting enabled, invoke ResetSmoothing().
public bool LimitSmoothed { get; set; }
Property Value
LimitTop
Top scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.
public int LimitTop { get; set; }
Property Value
Offset
The camera's relative offset. Useful for looking around or camera shake animations. The offsetted camera can go past the limits defined in LimitTop, LimitBottom, LimitLeft and LimitRight.
public Vector2 Offset { get; set; }
Property Value
PositionSmoothingEnabled
If true
, the camera's view smoothly moves towards its target position at PositionSmoothingSpeed.
public bool PositionSmoothingEnabled { get; set; }
Property Value
PositionSmoothingSpeed
Speed in pixels per second of the camera's smoothing effect when PositionSmoothingEnabled is true
.
public float PositionSmoothingSpeed { get; set; }
Property Value
ProcessCallback
The camera's process callback. See Camera2D.Camera2DProcessCallback.
public Camera2D.Camera2DProcessCallback ProcessCallback { get; set; }
Property Value
RotationSmoothingEnabled
If true
, the camera's view smoothly rotates, via asymptotic smoothing, to align with its target rotation at RotationSmoothingSpeed.
Note: This property has no effect if IgnoreRotation is true
.
public bool RotationSmoothingEnabled { get; set; }
Property Value
RotationSmoothingSpeed
The angular, asymptotic speed of the camera's rotation smoothing effect when RotationSmoothingEnabled is true
.
public float RotationSmoothingSpeed { get; set; }
Property Value
Zoom
The camera's zoom. A zoom of Vector(2, 2)
doubles the size seen in the viewport. A zoom of Vector(0.5, 0.5)
halves the size seen in the viewport.
Note:
Oversampling does not take Camera2D zoom into account. This means that zooming in/out will cause bitmap fonts and rasterized (non-MSDF) dynamic fonts to appear blurry or pixelated unless the font is part of a CanvasLayer that makes it ignore camera zoom. To ensure text remains crisp regardless of zoom, you can enable MSDF font rendering by enabling ProjectSettings.gui/theme/default_font_multichannel_signed_distance_field
(applies to the default project font only), or enabling Multichannel Signed Distance Field in the import options of a DynamicFont for custom fonts. On system fonts, MultichannelSignedDistanceField can be enabled in the inspector.
public Vector2 Zoom { get; set; }
Property Value
Methods
Align()
Aligns the camera to the tracked node.
public void Align()
ForceUpdateScroll()
Forces the camera to update scroll immediately.
public void ForceUpdateScroll()
GetScreenCenterPosition()
Returns the center of the screen from this camera's point of view, in global coordinates.
Note: The exact targeted position of the camera may be different. See GetTargetPosition().
public Vector2 GetScreenCenterPosition()
Returns
GetTargetPosition()
Returns this camera's target position, in global coordinates.
Note: The returned value is not the same as GlobalPosition, as it is affected by the drag properties. It is also not the same as the current position if PositionSmoothingEnabled is true
(see GetScreenCenterPosition()).
public Vector2 GetTargetPosition()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsCurrent()
Returns true
if this Camera2D is the active camera (see GetCamera2D()).
public bool IsCurrent()
Returns
MakeCurrent()
public void MakeCurrent()
ResetSmoothing()
Sets the camera's position immediately to its current smoothing destination.
This method has no effect if PositionSmoothingEnabled is false
.
public void ResetSmoothing()