A camera in Defold is a game object that controls what part of your game world the player sees on screen

Defold does not include a built-in camera system — you build one yourself using a collection, a script, and positioning math. The camera works by moving a viewport (the rectangle of game space that renders) to follow the player, stay centered on action, or show a fixed area. Without a camera, your game shows only a fixed slice of the world, which works for small games but breaks down quickly once your level is larger than the screen.

The most common approach is to create a camera game object that reads the player's position each frame and moves the viewport to keep the player centered. Defold's go.set function lets you move the camera by adjusting the position of a sprite or collection, and the viewport follows. You do not need plugins or external libraries — the camera is just a script that does math on positions and tells Defold where to look.

Key Takeaways

  • A Defold camera is a game object with a script that reads the player position and moves a viewport sprite to keep the action centered on screen.
  • You create the camera by making a new game object file, adding a sprite, writing a script that listens for player position messages, and adding the camera to your collection.
  • The camera script uses go.set to move the sprite position, which Defold treats as the center of the viewport.
  • You can add boundaries to stop the camera from showing empty space outside your level, or add smoothing so the camera eases into position instead of jumping.
  • The camera must be added to your main collection after the player, so it can read the player's position each frame.

Create a new game object for the camera

Start by making a new game object file. In the Defold editor, right-click your project folder, select New File, and choose Game Object. Name it camera.go. This file will hold the camera's sprite and script.

Open camera.go and add a sprite component. Click the + button next to Components, select Sprite, and point it to any sprite image — a small white square or circle works fine, since the camera sprite itself is invisible to the player. The sprite's position is what Defold uses to calculate the viewport center. Save the file.

Write the camera script

Create a new script file called camera.script. Right-click your project, select New File, choose Script, and name it. Open the file and paste this basic camera code:

go.property("target", hash("player")) go.property("lerp_speed", 0.1) function update(self, dt)   local target_id = msg.url(nil, self.target, "sprite")   local target_pos = go.get(target_id, "position")   local current_pos = go.get("#sprite", "position")   local new_pos = vmath.lerp(self.lerp_speed, current_pos, target_pos)   go.set("#sprite", "position", new_pos) end

This script does four things: it reads the target object's position (the player), gets the camera's current position, calculates a smoothed position between them using vmath.lerp, and moves the camera sprite to that new position. The lerp_speed property controls how fast the camera follows — lower values make it lag behind smoothly, higher values make it snap to the player faster.

Attach this script to camera.go by clicking the + button next to Script in the game object editor and selecting camera.script. Save both files.

Add the camera to your main collection

Open the collection file where your player exists (usually main.collection). Click the + button next to Instances and add camera.go. The order matters — make sure the player instance appears before the camera instance in the list, so the camera can read the player's position on the first frame.

Position the camera sprite at the center of your screen by setting its position to roughly the middle of your viewport (often around 400, 300 for a 800×600 game, depending on your project settings). Save the collection.

Set the viewport to follow the camera

Defold's viewport is controlled through the game.project file. Open it and find the Display section. The default viewport is fixed at the screen size. To make it follow your camera, you need to tell Defold to use the camera's position as the viewport center.

In most Defold projects, this is done by setting the viewport in code rather than in the project file. Add this to your main collection's script or to an init script that runs at startup:

local camera_pos = go.get("camera#sprite", "position") local viewport = vmath.vector3(camera_pos.x - 400, camera_pos.y - 300, 0) msg.post("@render:", "set_view", {position = viewport})

This tells the render system to center the viewport on the camera sprite. The numbers 400 and 300 are half your screen width and height — adjust them to match your game's resolution.

Add boundaries so the camera does not show empty space

If your level is smaller than the screen or has edges, the camera can pan past the level boundary and show black space. To prevent this, add clamping to the camera script. Modify the update function to limit the camera position:

local min_x, max_x = 400, 1600 local min_y, max_y = 300, 1200 new_pos.x = math.max(min_x, math.min(max_x, new_pos.x)) new_pos.y = math.max(min_y, math.min(max_y, new_pos.y)) go.set("#sprite", "position", new_pos)

Replace the numbers with your level's actual boundaries. If your level is 2000 pixels wide and 1600 pixels tall, and your screen is 800×600, set min_x to 400 (half the screen width) and max_x to 1600 (level width minus half screen width). The camera will stop at the edges and never show beyond them.

Test the camera and adjust smoothing

Run your game and move the player around. The camera should follow, staying centered on the player. If the camera feels jerky, lower the lerp_speed value in the camera script (try 0.05 or 0.08). If it feels sluggish, raise it (try 0.15 or 0.2). The right value depends on your game's speed and feel — there is no universal number.

If the camera does not move at all, check that the player instance name in your collection matches the target property in the camera script. By default it looks for an object named "player" — if your player is named something else, change the property value to match.

Frequently Asked Questions

Can I make the camera zoom in and out?

Yes. Add a zoom property to the camera script and use go.set("#sprite", "scale", zoom_value) to scale the sprite. You can tie zoom to the player's speed, a powerup, or a key press. The viewport will scale with the sprite, showing more or less of the world.

How do I make the camera follow multiple objects, like in a co-op game?

Calculate the center point between all players instead of following one. Use vmath.vector3 to average their positions, then move the camera to that average point. Add boundaries to keep the camera from zooming out too far.

What if my camera position is not updating?

Check that the player instance is in the collection before the camera instance, and that the player's name matches the camera script's target property. Also verify the player has a sprite component — the camera reads the sprite's position, not the game object's position.

Can I have multiple cameras in one game?

Defold supports only one active viewport at a time, so you cannot split the screen between two cameras. You can switch between cameras by changing which object the viewport follows, but only one can be active per frame.

How do I make the camera move to a specific location without the player?

Change the camera's target temporarily. Instead of following the player, set the target to a fixed position or a cutscene object. Use go.set to move the camera directly, or create a temporary target object at the location you want to show.