Table of Contents

Class Basic2DCameraController

Namespace
Stride.CommunityToolkit.Scripts
Assembly
Stride.CommunityToolkit.dll

Provides an interactive 2D camera controller for navigating 2D scenes in Stride. This controller supports movement in the XY-plane using the arrow keys (optionally WASD), zooming in and out with the mouse wheel, middle-mouse drag panning, optional screen edge panning, camera following, and smooth movement. Additional features include a speed boost when holding shift and the ability to reset the camera to a default position and zoom level using the 'H' key.

public class Basic2DCameraController : SyncScript, IIdentifiable, ICollectorHolder
Inheritance
Basic2DCameraController
Implements
Inherited Members
Extension Methods

Remarks

  • The camera moves at a configurable speed which can be increased with shift keys.
  • Zooming scales the camera's OrthographicSize by a fixed fraction per mouse-wheel notch; shift zooms faster too.
  • Optional features: screen edge movement, camera bounds, follow target, smooth movement, mouse drag panning.
  • The 'H' key resets the camera to its default position and orthographic size.
  • Default settings: FarClipPlane=1000, NearClipPlane=0.1f, OrthographicSize=10.

Properties

CameraMoveSpeed

Gets or sets the base speed of camera movement in units per second.

public float CameraMoveSpeed { get; set; }

Property Value

float

Remarks

This value is multiplied by SpeedFactor when shift keys are held.

EnableBounds

Gets or sets whether camera position bounds limiting is enabled.

public bool EnableBounds { get; set; }

Property Value

bool

Remarks

When enabled, the camera position is constrained between MinBounds and MaxBounds.

EnableMouseDragPan

Gets or sets whether mouse drag panning is enabled. Defaults to true.

public bool EnableMouseDragPan { get; set; }

Property Value

bool

Remarks

When enabled, holding MouseDragButton and moving the mouse drags the world with the cursor: the point under the cursor stays under the cursor.

EnableScreenEdgeMovement

Gets or sets whether RTS-style screen edge panning is enabled.

public bool EnableScreenEdgeMovement { get; set; }

Property Value

bool

Remarks

When enabled, moving the mouse cursor near screen edges will pan the camera in that direction.

EnableSmoothing

Gets or sets whether smooth camera movement with linear interpolation is enabled.

public bool EnableSmoothing { get; set; }

Property Value

bool

Remarks

When enabled, camera movement is smoothed using lerp based on SmoothingSpeed.

EnableWasdMovement

Gets or sets whether W, A, S and D move the camera in addition to the arrow keys. Defaults to false.

public bool EnableWasdMovement { get; set; }

Property Value

bool

Remarks

Off by default so that a game built on top of the toolkit keeps WASD for itself; the arrow keys are always active. Turn it on for tools and playgrounds where nothing else wants those keys.

FollowOffset

Gets or sets the offset from the follow target's position.

public Vector3 FollowOffset { get; set; }

Property Value

Vector3

Remarks

This offset is added to the FollowTarget position when calculating the camera's target position.

FollowSmoothing

Gets or sets the smoothing factor for the camera follow movement.

public float FollowSmoothing { get; set; }

Property Value

float

Remarks

A value of 0 results in instant following, while higher values produce smoother, more gradual movement.

FollowTarget

Gets or sets the entity for the camera to follow.

public Entity? FollowTarget { get; set; }

Property Value

Entity

Remarks

When set, the camera will automatically track this entity's position, applying FollowOffset and FollowSmoothing. Manual camera controls are disabled while following a target.

HelpCollapsed

Gets or sets whether the camera's help starts collapsed to its title line, leaving a one-line reminder of the key rather than the full list. Defaults to true, and must be set before the script starts.

public bool HelpCollapsed { get; set; }

Property Value

bool

Remarks

Collapsed by default because these keys are the same in every scene and stop being worth several lines of screen space almost immediately, while whatever the scene itself has to say does not. The remaining line names the key, so nothing is hidden without a way back.

HelpToggleKey

Gets or sets the key that collapses and expands the camera's help. Defaults to F2. Must be set before the script starts.

public Keys HelpToggleKey { get; set; }

Property Value

Keys

MaxBounds

Gets or sets the maximum camera position bounds in the XY-plane.

public Vector2 MaxBounds { get; set; }

Property Value

Vector2

Remarks

Only applies when EnableBounds is true.

MaxOrthographicSize

Gets or sets the maximum orthographic size, representing maximum zoom out.

public float MaxOrthographicSize { get; set; }

Property Value

float

MinBounds

Gets or sets the minimum camera position bounds in the XY-plane.

public Vector2 MinBounds { get; set; }

Property Value

Vector2

Remarks

Only applies when EnableBounds is true.

MinOrthographicSize

Gets or sets the minimum orthographic size, representing maximum zoom in.

public float MinOrthographicSize { get; set; }

Property Value

float

MouseDragButton

Gets or sets the mouse button used for drag panning.

public MouseButton MouseDragButton { get; set; }

Property Value

MouseButton

Remarks

Only applies when EnableMouseDragPan is true.

OrthographicSizeDefault

Gets or sets the orthographic size the 'H' key resets the camera to. null, the default, restores the size the camera had when the controller started - so a scene that set its own framing gets that framing back, not a fixed number.

public float? OrthographicSizeDefault { get; set; }

Property Value

float?

ScreenEdgeBorderWidth

Gets or sets the width in pixels of the screen edge border that triggers camera movement.

public float ScreenEdgeBorderWidth { get; set; }

Property Value

float

Remarks

Only applies when EnableScreenEdgeMovement is true.

ShowInstructions

Gets or sets whether on-screen camera instructions are displayed.

public bool ShowInstructions { get; set; }

Property Value

bool

Remarks

This hides only the camera's own lines. The overlay itself, and anything else contributing to it, is toggled with ToggleKey.

SmoothingSpeed

Gets or sets the speed of smooth movement interpolation.

public float SmoothingSpeed { get; set; }

Property Value

float

Remarks

Higher values result in faster interpolation towards the target position. Only applies when EnableSmoothing is true.

SpeedFactor

Gets or sets the speed multiplier applied when holding shift keys.

public float SpeedFactor { get; set; }

Property Value

float

Remarks

The effective movement speed becomes CameraMoveSpeed * SpeedFactor when either shift key is pressed, and each mouse-wheel notch counts as SpeedFactor notches of ZoomStep.

ZoomStep

Gets or sets the fraction by which the visible area scales per mouse-wheel notch. Defaults to 0.1 (10 %).

public float ZoomStep { get; set; }

Property Value

float

Remarks

Zoom is multiplicative, so every notch changes the view by the same proportion whether the camera is zoomed far in or far out. Wheel input is an impulse rather than a held key, so it is not scaled by delta time. Holding shift multiplies the notch count by SpeedFactor.

ZoomToCursor

Gets or sets whether zooming keeps the world point under the cursor fixed, the way map and canvas applications do. Defaults to true; set to false for the old behaviour of zooming about the centre of the screen.

public bool ZoomToCursor { get; set; }

Property Value

bool

Remarks

With smoothing enabled the position shift is applied to the smoothing target, so the anchor is approximate while the zoom eases and exact once it settles.

Methods

Start()

Initializes the camera controller by setting up the instruction overlay and caching the initial state.

public override void Start()

Remarks

This method sets the target position to the current camera position and configures the debug text printer for displaying on-screen instructions. Called once when the script starts. The position captured here is the one the 'H' key restores, so a camera created at a custom position resets to that position rather than to a fixed default.

Update()

Updates the camera controller state every frame, handling movement, zoom, following, bounds, and instruction display.

public override void Update()

Remarks

The update order is as follows:

  1. Cache the camera component reference if not already cached.
  2. Process instruction toggle keys (F2, F3).
  3. Process camera follow if FollowTarget is set, otherwise process manual controls (movement, screen edge, mouse drag).
  4. Process camera zoom via mouse wheel.
  5. Check for camera reset (H key).
  6. Apply smooth movement if EnableSmoothing is enabled.
  7. Apply camera bounds if EnableBounds is enabled.
  8. Display instructions if visible.