Understanding how movement works related to the game camera

I have created a sample project to illustrate the issue I am seeing:

In this sample, I have:

  • A ‘player’ that is moving constantly up and down
  • An orthographic camera that is following the player smoothly
  • Another object (a duck) is adjusting its position to match the camera movement.

Theoretically, this other object should look as it is not moving, but if you run the project you will see a movement on it, although I am adjusting its position to always match the position of the camera.

This is a sample project extracted from a bigger one, in which I am having some strange issues creating a parallax effect.

Does anyone know what am I doing wrong? Which adjustments should I do to give an effect that the other object is static?

Without looking my guess is that this part is the problem.

Why not attach that object as a child to the camera to have it always follow the movement of the camera?

Without looking my guess is that this part is the problem.

How can this be a part of the problem? I have implemented this same pattern in other engines without issues.

Why not attach that object as a child to the camera to have it always follow the movement of the camera?

Because they are different things. This is just a sample but in my other project, one thing is the camera and another is the background, and I want them separately because their must move separately at some point.

Well, it all depends on which order your scripts are processed by the engine. If the duck in your example updates before the other scripts it will adjust to the previous position of the camera. I would recommend that you do it post update:

-- duck.script
function update(self, dt)
	msg.post("#", "follow_camera")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("follow_camera") then 
		local camera_pos = go.get_position(camera_id)
		local y_position = camera_pos.y
		go.set_position(vmath.vector3(self.x, y_position, self.z))
	end
end
2 Likes

This fixes the issue for the sample but I have tried this in my other project and something is wrong. I will keep trying, thanks.