Top-Down Camera
RTS/strategy camera with WASD panning, edge scrolling, scroll wheel zoom, and optional target follow.
No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked
What it adds to your GameObject
A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 14 serialized fields with their defaults.
When to use it
Designed for RTS, city builders, tower defense, and top-down RPGs where the camera looks down at the action. Use for strategy games needing pan, zoom, and optional unit following. Also works for isometric games — just angle the camera rig accordingly.
Setup
- Create a camera rig at a top-down angle (e.g., rotation 60,0,0)
- Attach TopDownCamera to the rig
- Use WASD or arrow keys to pan
- Scroll wheel to zoom in/out
- Enable edge scrolling for mouse-at-screen-edge panning
- Set boundaries to limit camera movement area
- Optionally assign a follow target for unit tracking
Features
- WASD/arrow key panning with configurable speed and smoothing
- Edge-of-screen mouse scrolling with adjustable zone size
- Scroll wheel zoom with min/max limits and smooth interpolation
- Optional rectangular boundary clamping for map edges
- Follow target mode for tracking a unit or player
- FocusOn method to snap camera to any world position
Inspector fields
Every field Top-Down Camera exposes in the Inspector, with the default it starts at.
| Field | Type | Default | Group |
|---|---|---|---|
Pan SpeedpanSpeed | float | 20 | Movement |
Pan SmoothingpanSmoothing | float | 8 | Movement |
Enable Edge ScrollenableEdgeScroll | bool | true | Edge Scrolling |
Edge Scroll ZoneedgeScrollZone | float | 20 | Edge Scrolling |
Edge Scroll SpeededgeScrollSpeed | float | 15 | Edge Scrolling |
Zoom SpeedzoomSpeed | float | 5 | Zoom |
Min ZoomminZoom | float | 5 | Zoom |
Max ZoommaxZoom | float | 30 | Zoom |
Zoom SmoothingzoomSmoothing | float | 8 | Zoom |
Use BoundariesuseBoundaries | bool | false | Boundaries |
Min BoundsminBounds | Vector2 | (-50, -50) | Boundaries |
Max BoundsmaxBounds | Vector2 | (50, 50) | Boundaries |
Follow TargetfollowTarget | Transform | None | Follow Target |
Follow SmoothingfollowSmoothing | float | 5 | Follow Target |
Public API
What other scripts can call on TopDownCamera.
| Member | Returns | Description |
|---|---|---|
FocusOn(Vector3 position) | void | Snap camera to a world position. |
SetFollowTarget(Transform target) | void | Set a target to follow. Pass null to stop following. |
Requirements
- Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.
Common mistakes
The camera rig needs to be angled looking down (e.g., X rotation 45-90 degrees) before attaching the script — it controls position, not rotation. Edge scrolling uses screen pixel coordinates, so edgeScrollZone should be small (10-30). If zoom feels wrong, check whether the camera is orthographic or perspective — the script handles both differently.
Source code
TopDownCamera.cs, 132 lines, MIT licensed. Paste it into a new C# file of the same name.
using UnityEngine;
/// <summary>
/// Top-down / RTS camera with WASD panning, edge scrolling, and zoom.
/// Perfect for strategy, management, and top-down games.
/// </summary>
public class TopDownCamera : MonoBehaviour
{
[Header("Movement")]
[SerializeField] private float panSpeed = 20f;
[SerializeField] private float panSmoothing = 8f;
[Header("Edge Scrolling")]
[SerializeField] private bool enableEdgeScroll = true;
[SerializeField] private float edgeScrollZone = 20f;
[SerializeField] private float edgeScrollSpeed = 15f;
[Header("Zoom")]
[SerializeField] private float zoomSpeed = 5f;
[SerializeField] private float minZoom = 5f;
[SerializeField] private float maxZoom = 30f;
[SerializeField] private float zoomSmoothing = 8f;
[Header("Boundaries")]
[SerializeField] private bool useBoundaries = false;
[SerializeField] private Vector2 minBounds = new Vector2(-50f, -50f);
[SerializeField] private Vector2 maxBounds = new Vector2(50f, 50f);
[Header("Follow Target")]
[SerializeField] private Transform followTarget;
[SerializeField] private float followSmoothing = 5f;
private Vector3 targetPosition;
private float targetZoom;
private Camera cam;
private void Awake()
{
cam = GetComponentInChildren<Camera>();
if (cam == null) cam = Camera.main;
targetPosition = transform.position;
targetZoom = cam.orthographic ? cam.orthographicSize : transform.position.y;
}
private void Update()
{
if (followTarget != null)
{
Vector3 followPos = followTarget.position;
followPos.y = transform.position.y;
targetPosition = Vector3.Lerp(targetPosition, followPos, followSmoothing * Time.deltaTime);
}
else
{
HandlePanInput();
HandleEdgeScroll();
}
HandleZoom();
ApplyMovement();
}
private void HandlePanInput()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 panDir = new Vector3(h, 0f, v).normalized;
targetPosition += panDir * panSpeed * Time.deltaTime;
}
private void HandleEdgeScroll()
{
if (!enableEdgeScroll) return;
Vector3 mousePos = Input.mousePosition;
Vector3 edgeDir = Vector3.zero;
if (mousePos.x < edgeScrollZone) edgeDir.x = -1f;
else if (mousePos.x > Screen.width - edgeScrollZone) edgeDir.x = 1f;
if (mousePos.y < edgeScrollZone) edgeDir.z = -1f;
else if (mousePos.y > Screen.height - edgeScrollZone) edgeDir.z = 1f;
targetPosition += edgeDir.normalized * edgeScrollSpeed * Time.deltaTime;
}
private void HandleZoom()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (Mathf.Abs(scroll) > 0.01f)
{
targetZoom -= scroll * zoomSpeed;
targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);
}
if (cam.orthographic)
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * zoomSmoothing);
else
{
Vector3 pos = transform.position;
pos.y = Mathf.Lerp(pos.y, targetZoom, Time.deltaTime * zoomSmoothing);
transform.position = pos;
}
}
private void ApplyMovement()
{
if (useBoundaries)
{
targetPosition.x = Mathf.Clamp(targetPosition.x, minBounds.x, maxBounds.x);
targetPosition.z = Mathf.Clamp(targetPosition.z, minBounds.y, maxBounds.y);
}
Vector3 smoothed = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * panSmoothing);
smoothed.y = transform.position.y;
transform.position = smoothed;
}
/// <summary>Snap camera to a world position.</summary>
public void FocusOn(Vector3 position)
{
targetPosition = new Vector3(position.x, transform.position.y, position.z);
}
/// <summary>Set a target to follow. Pass null to stop following.</summary>
public void SetFollowTarget(Transform target)
{
followTarget = target;
}
}