How to get camera to centre on the player?

At the moment i’ve got a camera to follow the player as it traverses the level. THis is good but i would like the camera to be centered on the player as it moves. This is bceuase i’d like to zoom in on the player to make the game look better. Right now when i play my game it looks like this:

#
Below is the code in the camera script:

function init(self)
	msg.post("camera#level1", "acquire_camera_focus")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("follow") then 
		go.set_parent(".", sender)
		go.set_position(vmath.vector3(512, 382.5, 0))
	elseif message_id == hash("unfollow") then
		go.set_parent("camera#level1", nil, true)
	end
end

This is the code in the player script:

	if action_id == hash("touch") and action.pressed then
		self.follow = not self.follow
		if self.follow then
			msg.post("camera#level1", "follow")
		else
			msg.post("camera#level1", "unfollow")
		end
	end

Do you guys know how i cen get the camera to center on the player. I’ve been trying some things out and i think the answer may lie in the line of code: go.set_position(vmath.vector3(512, 382.5, 0)). Right now its set to 1/2 the aspect ratio which i thought would cause it to be in the center but evidently not. I also tried to use the URL for the player sprite but this didn’t seem to help?

note that "anchor "of camera is the left bottom corner. try this:

local w = sys.get_config_number("display.width", 0)
local h = sys.get_config_number("display.height", 0)

-- say you want to look at targetx, targety
go.set_position(vmath.vector3(targetx- w/2, targety-h/2, 0))
1 Like

You didn’t mention which camera solution you are using. If you use the built-in camera and set it to use an orthographic projection it should automatically center on the game object it is attached to:

1 Like