Adding a camera to your Defold game
A camera in Defold is a game object that controls what part of your game world the player sees on screen. To add one, you create a new game object file, give it a camera component, and then spawn that game object in your collection. The camera then follows your player or moves to show specific areas of the map.
Start by right-clicking in your file tree and selecting New File > Game Object. Name it something like camera.go. This creates a blank game object file where you'll add the camera component.
Next, you need to add a camera component to this game object. In the same folder, create a new file by right-clicking and selecting New File > Camera. Name it camera.camera. This file defines how the camera behaves — its aspect ratio, near and far clipping planes, and projection type. For most 2D games, the default settings work fine.
Key Takeaways
- A camera in Defold is a game object with a camera component that determines what the player sees on screen.
- Create a camera game object by making a new .go file, then add a .camera component to it.
- Spawn the camera in your collection and use a script to move it based on player position or other game events.
- Use go.set() to change the camera's position in real time, or write a script that updates the position every frame.
Linking the camera component to your game object
Open your camera.go file. In the editor, you'll see a text area where you can add components. Add a line that references your camera component:
components { id: "camera" component: "/path/to/camera.camera" }
Replace /path/to/ with the actual folder path where you saved your camera.camera file. If both files are in the same folder, you can write just "camera.camera".
You also need to add a script to the game object so it can receive messages and move the camera. Create a new script file in the same folder — right-click, select New File > Script, and name it camera.script. Then add it to your game object file:
components { id: "camera" component: "camera.camera" } components { id: "script" component: "camera.script" }
Writing a script to move the camera
The script is where you tell the camera where to look. Open your camera.script file. A basic script that follows the player looks like this:
go.property("target", hash("player")) function update(self, dt) local target_id = self.target local target_pos = go.get("#" .. target_id, "position") go.set(".", "position", target_pos) end
This script reads the position of a game object named "player" every frame and moves the camera to that same position. The go.property() line at the top lets you change which object the camera follows without editing the script — you can set it in the editor.
If you want the camera to lag slightly behind the player (a common effect), use linear interpolation:
go.property("target", hash("player")) go.property("lerp_speed", 0.1) function update(self, dt) local target_id = self.target local target_pos = go.get("#" .. target_id, "position") local current_pos = go.get(".", "position") local new_pos = vmath.lerp(self.lerp_speed, current_pos, target_pos) go.set(".", "position", new_pos) end
The lerp_speed value controls how quickly the camera catches up — lower values make it lag more smoothly.
Spawning the camera in your collection
Your camera game object won't do anything until you spawn it in a collection. Open the collection where your player and other game objects live. Right-click in the collection editor and select Add Game Object. Choose your camera.go file. Place it anywhere in the editor — the position doesn't matter because the script will move it to follow the player.
Make sure the camera is spawned before or at the same time as the player. If the player spawns first and the camera spawns later, there will be a frame where the camera hasn't caught up yet.
Adjusting camera settings for your game
Open your camera.camera file to tweak how the camera behaves. The Aspect Ratio setting controls the width-to-height ratio of what the camera sees. For a mobile game in portrait mode, you might use 9:16. For a desktop game in landscape, 16:9 is common.
The Near Z and Far Z values control which layers of your game are visible. Objects closer than Near Z or farther than Far Z won't be drawn. For most 2D games, the defaults work fine, but if you have multiple layers at different depths, you may need to adjust these.
The FOV (field of view) setting controls how much of the world the camera sees. A higher FOV shows more of the world but makes objects appear smaller. A lower FOV zooms in on a smaller area.
Common camera problems and fixes
If the camera doesn't move, check that the script is actually running. Open the console (press F8 in the editor) and look for errors. A common mistake is misspelling the target game object name in the script.
If the camera shows a black screen, the camera's position might be outside the bounds of your game world, or the Near Z and Far Z values might be cutting off all your objects. Try setting the camera position to the same position as your player and verify that your game objects are within the Near Z and Far Z range.
If the camera jitters or shakes, you might be updating the camera position in both the camera script and somewhere else (like the player script). Remove the duplicate update so the camera only moves from one place.
Frequently Asked Questions
Can I have multiple cameras in one game?
Yes, but only one camera can be active at a time. You can switch between cameras by disabling one and enabling another, or by changing which game object the active camera follows. Use msg.post() to send messages between cameras and your game logic.
How do I zoom the camera in and out?
Modify the camera's scale property in the script. Use go.set(".", "scale", 2) to zoom in by 2x, or go.set(".", "scale", 0.5) to zoom out. You can animate this over time using go.animate() for smooth zoom effects.
What if I want the camera to stay within map boundaries?
Clamp the camera position to your map's min and max coordinates in the script. Before setting the camera position, check if it would go outside the map and adjust it: new_pos.x = math.max(min_x, math.min(max_x, new_pos.x)). Do the same for the y-axis.
Can the camera follow something other than the player?
Yes. Change the target property in the script to point to any game object. You can also make the camera follow a fixed point, an enemy, or even an invisible marker that you move manually with go.set().