Ahem. Good afternoon my fellow developers. I have a very important question, which hopefully can be solved.
For my game, I have a camera which moves with the arrow keys, and it works well; however, the collision objects for my sprites do not stay with the actual objects. When the camera moves, they remain in the same position in the camera view, rather than stay fixed onto the game object like they should. How can I fix this?
So this is the main screen. The game is heavily inspired by Europa Universalis, if you’re familiar with those games. Now, if I press the arrow keys, I can move the camera, like so:
But when the camera is moved, the sprites move across the screen and whatnot, but the collision objects remained fixed in the same position on the screen (so if the collision box was in the middle of the screen, no matter where you moved, the collision box would always register in the middle of the screen, even if its sprite was no longer visible on screen).
Are you moving the camera or are you moving everything else? If you’re moving the game objects and the collision objets are static then those will stay in place while the sprites and other components follow the game objects.
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("#camera", "acquire_camera_focus")
end
function on_input(self, action_id, action)
local cam = go.get_position("camera")
if action_id == hash("right") then
go.set_position(cam + vmath.vector3(10,0,0), "camera")
end
if action_id == hash("left") then
go.set_position(cam + vmath.vector3(-10,0,0), "camera")
end
if action_id == hash("up") then
go.set_position(cam + vmath.vector3(0,10,0), "camera")
end
if action_id == hash("down") then
go.set_position(cam + vmath.vector3(0,-10,0), "camera")
end
end
The sprites are under the “provinces” game object. You can also see the collision objects (I forgot to show the sprite for the little army dude, but same concept applies).
I’d recommend to use either Defold-Orthographic or RenderCam (both in the Asset Portal). Both are camera solutions with accompanying render scripts that support zoom, projections etc. Defold-Orthographic is 2D only while RenderCam supports both 2D and 3D projections.