Skip to content
Guilherme Sousa edited this page May 1, 2023 · 2 revisions

Camera

So turns out a good camera implementation is important to make a game feel alive and fun to play, who new? A few common camera behaviors are available on this game base:

Camera Targeting

Cameras usually follow things. You may be thinking, "how is this complicated? just parent the camera to the thing you want it to follow". This approach is usually a bad idea and brings issues down the line:

  • What if I want the camera to follow two things?
  • What if I decide to flip to make it face to the left, the camera gets flipped as well?

Its usually best to have the camera be simply parented to the world node, and let a script handle the following of things. The CameraTargeter does just that:

image

By giving it a reference to the camera and a list of Node2D to follow, the camera will move and adjust its zoom values to try to always have all of its targets in camera.

Camera Shake

Shaking the camera is a great way of giving the player feedback. The CameraShaker node applies random noise to a Camera2D offset and rotation. Simply call its add_trauma method every time an even is triggered that requires camera shake. The trauma amount will then be accumulated and decayed over time. This node does not affect the camera position and can be used in conjunction with CameraTargeter.

Camera Areas

Sometime, we want cameras to stay within certain limits in one place, and within other limits in another. That's where CameraArea come in, this node will confine the camera to only show the contents of an area (when possible) once the player enters that area. You can set this up quite easily by:

  1. Adding a res://scenes/common/camera/camera_area.tscn to your scene.
  2. Adding a collision shape to that area. The shape should be a RectangleShape2D named CollisionShape2D.

image

This should automatically confine the current camera once the player enter the area. Note that game events are assigned to the Area Enter and Area Exit fields. You can use these to add custom behavior when a player enters/leaves an area (such as updating the minimap)

Minimap

There are several ways to implement a minimap. This one uses a subviewport to render specific culling layers of the game world onto a texture. Here is how the setup on res://scenes/samples/platformer_sample.tscn works:

  • The minimap subviewport contains a camera that renders the "Minimap" culling layer only. This camera has a script to always be centered on the current CameraArea. You can implement you own behavior to move this camera (like using a CameraTargeter instead), just keep in mind that the minimap will only display what this camera can see.

image

  • Once that is setup, you can easily get the contents of your minimap viewport by using a ViewportTexture. Here that can be found on the minimap control node:

image image

  • Since we only want to show the tilemap on the minimap, we add it to the "Minimap" culling layer. If you want anything else to show up on the minimap just add it to this layer as well!

image

  • This alone already give you a minimap that shows exactly what is rendered on screen. However we often want stylized and low detail renders on our minimap. In this case the minimap_texture node is also using a simple shader that draws the background as a color and the tileset as another.

image

Clone this wiki locally