How to Add a Camera in Defold: A Complete Guide
Defold handles cameras differently from many other game engines, and that surprises a lot of developers coming from Unity or Godot. Understanding the system properly — rather than just copying a snippet — will save you significant debugging time and give you real control over how your game world is rendered.
What a Camera Actually Does in Defold
In Defold, a camera component doesn't move the world or transform your game objects directly. Instead, it communicates with the render pipeline to define what portion of the game world gets drawn to the screen and how.
When a camera is active, it sends view and projection matrices to the render script. Those matrices tell the renderer which coordinate space to use when drawing. Without a camera sending those messages, the render script falls back to default behavior — typically a static view anchored to the world origin.
This distinction matters because adding a camera in Defold involves two separate concerns:
- Placing the camera in your game scene
- Ensuring the render script listens to the camera
Both steps are necessary. Many developers add the camera component correctly but wonder why nothing changes — it's usually because the render script isn't configured to use it.
Step 1: Add the Camera Component to a Game Object
To add a camera in Defold:
- Open or create a game object (
.gofile) in your project, or add the camera directly inside a collection. - Right-click the game object in the Outline panel and select Add Component.
- Choose Camera from the component list.
- The camera component will appear with the following configurable properties:
| Property | What It Controls |
|---|---|
| Aspect Ratio | Width-to-height ratio of the rendered view |
| FOV (Field of View) | Vertical angle of the view frustum (3D games) |
| Near Z / Far Z | Depth clipping planes — what's too close or too far to render |
| Auto Aspect Ratio | Automatically match the window's current aspect ratio |
| Orthographic Projection | Enables flat 2D-style rendering with no perspective |
| Orthographic Zoom | Scales the view in orthographic mode |
For 2D games, orthographic projection is almost always the correct choice. For 3D games, perspective projection using FOV gives objects a sense of depth.
Step 2: Acquire the Camera from a Script
Simply placing a camera component isn't enough to activate it. You need to send an acquire_camera_focus message from a script attached to the same game object (or a nearby one that references it).
function init(self) msg.post("#camera", "acquire_camera_focus") end The #camera reference assumes your camera component is named "camera" — check the component's ID in the Outline panel if it isn't responding. Only one camera can hold focus at a time, so if you're switching between cameras (e.g., cutscenes, split-screen, or level transitions), you'll need to explicitly acquire and release focus.
To release focus:
msg.post("#camera", "release_camera_focus") Step 3: Update the Render Script to Use Camera Data 🎥
This is the step most tutorials gloss over. Defold's default render script — found at /builtins/render/default.render_script — does not automatically use a camera component's matrices.
To use your camera properly, you have two options:
Option A — Use the built-in camera render message handler Defold's render script can be configured to listen for set_view_projection messages, which the camera component automatically sends when it has focus. The default render script already handles this in many versions of Defold, but you should verify your render script includes:
function on_message(self, message_id, message) if message_id == hash("set_view_projection") then self.view = message.view self.projection = message.projection end end And that those matrices are applied during rendering:
render.set_view(self.view) render.set_projection(self.projection) Option B — Use a Camera Library For more complex needs — smooth follow cameras, screen shake, zoom, viewport management — many Defold developers use the community Rendercam or Orthographic Camera libraries available through the Defold Asset Portal. These handle the render script integration for you and expose cleaner APIs for common camera behaviors.
Step 4: Attach the Camera to a Moving Object
If you want the camera to follow a player character, attach the camera component to the same game object as the player, or use a dedicated camera game object and update its position in a script:
function update(self, dt) local player_pos = go.get_position("/player") go.set_position(player_pos, "/camera_object") end This approach gives you full control to add offsets, lerp for smooth following, or clamp to level boundaries.
Variables That Affect How You Set This Up
How you configure your camera depends heavily on your project specifics:
- 2D vs 3D — orthographic vs perspective projection changes almost every relevant setting
- Resolution and aspect ratio targets — fixed resolution games behave differently from games that support multiple screen sizes
- Whether you're using a custom render script — if you've already modified the render pipeline, camera integration requires matching that existing setup
- Camera behaviors needed — a static background camera is trivial; a camera with zoom, shake, and smooth follow requires significantly more script logic
- Defold version — the engine has evolved, and camera handling behavior has changed across versions; checking the release notes for your specific version is worth the time
The right approach for a simple top-down 2D game looks almost nothing like the setup for a 3D game with dynamic camera cuts — and even within 2D projects, whether you need pixel-perfect rendering or fluid zoom determines which libraries or render script modifications make sense for your situation.