Use message-based API for camera control
mastermsg.post. This allows you to control the camera from other game objects without needing direct access to the camera's Lua module.repository·master·Indexed 42 days ago
https://github.com/britzl/defold-orthographicAn orthographic camera API for the Defold game engine that extends standard camera capabilities. It provides functionality for following game objects, managing camera bounds and deadzones, and implementing visual effects such as screen shake and recoil. The API is accessible via the camera.lua module or a message-based system using msg.post to the camera.script.
msg.post. This allows you to control the camera from other game objects without needing direct access to the camera's Lua module.The Orthographic Camera API can be accessed in two ways:
camera.lua module: Call functions like camera.shake() or camera.follow() in your Lua code.camera.script attached to your camera game object.orthographic/render/orthographic.render_script.To use the orthographic camera in your Defold project, add it as a library dependency in your game.project file. Under the dependencies field, add the following URL:
https://github.com/britzl/defold-orthographic/archive/master.zip
Alternatively, you can point to a specific release ZIP file from the GitHub releases page.
https://github.com/britzl/defold-orthographic/archive/master.zipcamera.go game object to your collection.camera.script to that game object.Important Note on accessing properties:
When using Defold's built-in go.set(), go.get(), or go.animate() functions, you must specify the URL to the camera script, not just the game object. For example, if your game object is named mycamera, use mycamera#camerascript.
go.animate("mycamera#camerascript", "zoom", ...)
go.set("mycamera#camerascript", "zoom")
go.get("mycamera#camerascript", "zoom")Use follow and follow_offset messages to make the camera track a target object.
Follow target: Accepted message keys:
target: The hash of the target object.lerp: Linear interpolation factor for smoothing.horizontal: Boolean to enable/disable horizontal tracking.vertical: Boolean to enable/disable vertical tracking.immediate: Boolean to snap to target immediately.offset: A vmath.vector3 for target offset.Follow with offset: Accepted message keys:
offset: A vmath.vector3 for the tracking offset.Stop following:
Send the unfollow message to stop tracking.
-- Follow a player with smoothing
msg.post("camera", "follow", { target = hash("player"), lerp = 0.7, horizontal = true, vertical = false, immediate = true })
-- Follow with a specific offset
msg.post("camera", "follow_offset", { offset = vmath.vector3(150, 250, 0) })
-- Stop following
msg.post("camera", "unfollow")The API provides utilities to translate coordinates between the screen (input/GUI) and the game world.
camera.screen_to_world(camera_id, screen) to convert a vector3 screen coordinate to world coordinates.camera.screen_to_world_bounds(camera_id) to get the world coordinates of the screen corners (returns a vector4: x=left, y=top, z=right, w=bottom).camera.world_to_screen(camera_id, world, [adjust_mode]) to convert a vector3 world coordinate to screen coordinates. This is useful for positioning GUI elements over game objects.local world_pos = camera.screen_to_world("mycamera#camerascript", vmath.vector3(input.get_touch_x(), input.get_touch_y(), 0))
local screen_pos = camera.world_to_screen("mycamera#camerascript", vmath.vector3(player_pos.x, player_pos.y, 0))Send a shake message to the camera component to initiate a screen shake effect. You can specify the intensity, duration, and direction.
Accepted message keys:
intensity: Float value for shake strength.duration: How long the shake lasts.direction: The axis/pattern of the shake (e.g., "both").When the shake finishes, the camera component sends a shake_complete message back to the sender.
msg.post("camera", "shake", { intensity = 0.05, duration = 2.5, direction = "both" })Control the active state and projection type of the camera.
Enable/Disable:
enable: Activates the camera. While enabled, it updates view/projection and sends them to the render script.disable: Deactivates the camera.Set projection: Accepted message keys:
projection: A hash representing the projection type (e.g., hash("FIXED_AUTO")).msg.post("camera", "enable")
msg.post("camera", "disable")
msg.post("camera", "use_projection", { projection = hash("FIXED_AUTO") })Send a recoil message to create a sudden offset effect.
Accepted message keys:
offset: A vmath.vector3 representing the displacement.duration: How long the recoil transition lasts.msg.post("camera", "recoil", { offset = vmath.vector3(100, 100, 0), duration = 0.75 })Manage camera zoom levels and automatic zoom behavior.
Zoom to specific level: Accepted message keys:
zoom: The target zoom level.Set automatic zoom: Accepted message keys:
enabled: Boolean to enable or disable automatic zoom.-- Zoom to 2.5x
msg.post("camera", "zoom_to", { zoom = 2.5 })
-- Enable automatic zoom
msg.post("camera", "set_automatic_zoom", { enabled = true })Adjust the area within which the target can move before the camera reacts, or set the camera's movement boundaries.
Deadzone: Accepted message keys:
left, right, bottom, top: Pixel values defining the deadzone area.Bounds: Accepted message keys:
left, right, bottom, top: Pixel values defining the camera movement limits.-- Set deadzone
msg.post("camera", "deadzone", { left = 10, right = 200, bottom = 10, top = 100 })
-- Set camera bounds
msg.post("camera", "bounds", { left = 10, right = 200, bottom = 10, top = 100 })