Skip to content

Camera

The PlayerCamera prefab is an orthographic camera that shows the map at a whole number of screen pixels per texture pixel, follows the player and stays inside the map. This page covers its two components and the View Camera field on the map.

Concept

Two components share the work:

  • Camera2DController sets the camera size from Zoom, lines the view up with the screen pixel grid and keeps the view inside the map.
  • FollowObjectBehaviour moves the camera smoothly towards a target.

Both are in Scripts/Extra/. The prefab is tagged MainCamera, which the touch controls need.

How to use it

  1. Delete the scene's default camera, or untag it.
  2. Drag Samples/Prefabs/PlayerCamera into the scene.
  3. Select the map and drag the camera into its View Camera field.
  4. Add a player. The camera follows the first PlayerController it finds when the scene starts.

Camera2DController

Field What it does
Zoom Screen pixels per texture pixel. At 1, one pixel of a 32 pixel tile covers one pixel of the screen; at 2, a 2 by 2 block. Values below 1 zoom out.
Pixel To Units Texture pixels per world unit. Leave it at 100 for the default map, where a 32 pixel tile is 0.32 units wide. If you change the map's cell size, set this to the tile width in pixels divided by the cell width.
Keep Inside Map Bounds Stops the view at the edges of the map. When the map is smaller than the view on one axis, the camera centres on it instead.

What pixel perfect means here

Every frame, after everything has moved, the controller sets the camera's orthographic size to screen height divided by 2 x Zoom x Pixel To Units. The Size you type on the Camera component is overwritten, so use Zoom instead.

It also shifts the camera by less than one screen pixel, so the corner of the view sits on a multiple of one screen pixel in world space. Tiles then land on whole pixels and don't shimmer while the camera scrolls.

For crisp pixels, keep Zoom a whole number (1, 2, 3) and import your textures with Filter Mode set to Point.

The in-game map editor uses this component too. The mouse wheel changes Zoom in whole steps up to 10 (and halves it below 1), and dragging with the middle mouse button moves the camera. If the map's View Camera has no Camera2DController when the in-game editor starts, it adds one.

FollowObjectBehaviour

Field What it does
Damp Time Roughly how long, in seconds, the camera takes to catch up with the target. 0 locks it on.
Target The object to follow. Left empty, Camera2DController fills it with the scene's PlayerController when the scene starts.

PlayerController also sets itself as the target when it starts and again whenever it moves, so the camera returns to the player after you have dragged it away in the in-game editor.

To follow something else, set the target from a script:

using UnityEngine;
using CreativeSpore.RpgMapEditor;

public class CutsceneCamera : MonoBehaviour
{
    [SerializeField] private Transform m_focus;

    public void LookAtFocus()
    {
        FollowObjectBehaviour follow = FindAnyObjectByType<FollowObjectBehaviour>();
        follow.Target = m_focus;
    }
}

The player takes the camera back as soon as it moves, so for a cutscene stop the player's input first.

The View Camera field

View Camera is the first field of the Settings tab in the map inspector. It tells the map which camera shows it in play mode. The in-game map editor uses it to find the tile under the mouse, and so does RpgMapHelper.GetMouseWorldPosition. If it is empty when the in-game editor starts, the map uses the camera tagged MainCamera and logs a warning.

Start Painting in the welcome window sets it for you on the scene it creates.