Skip to content
Download

Camera following the player

You have a room bigger than the screen and want the camera to follow the player. You can do that without code, in the editor.

  1. Make the room bigger: select it in Recursos (Resources) and, in the Inspetor (Inspector), change Tamanho › Largura (size › width) and Altura (height). For example, 960 × 180 for a long level.
  2. Still in the room’s Inspector, open Câmera and choose the object in Seguir (follow), for example Player.
  3. Press Play: the camera centers on the player and follows it.
Option What it does
Visão (view) The size of the area that shows on screen. With Do projeto (from the project), it is the resolution of the game; with Própria (its own), you choose the size.
Seguir (follow) The object the camera follows, or (nenhum) (none).
Limitar à room (limit to the room) The camera shows nothing outside the room, not even when the player reaches the edge.
Suavização (smoothing) From 0 to 10. At 0, the camera sticks to the player; the higher it is, the more smoothly it arrives.
Zona morta (dead zone) A rectangle in the middle of the screen in which the player moves without the camera moving.

In the room view, a dashed rectangle shows the area of the camera. See Room camera.

The camera is also accessible from the code, through the current room:

void onSpawn() override {
scene().camera.follow(handle()); // follow this instance
scene().camera.smoothing = 4;
scene().camera.bounded = true; // limit to the room
}

scene().camera.stopFollowing() stops following. With no target, you move the camera by hand by changing scene().camera.x and scene().camera.y (the top left corner of the view).