Problems with rendercam

My character should always look at the mouse cursor, but after the character moves, he stops looking at the cursor.

function look_at(target_position, my_position, turn_object)
	local angle = math.atan2(my_position.x - target_position.x, target_position.y - my_position.y)
	go.set_rotation(vmath.quat_rotation_z(angle), turn_object)
end

function on_input(self, action_id, action)
	local target_position = rendercam.screen_to_world_2d(action.screen_x, action.screen_y)
	look_at(target_position, go.get_position("/Player/visual"), "/Player/visual")
	go.set_position(target_position, "/Player/go")

	local dt = 1/60

	if action_id == hash("W") and not Console then
		go.set_position(vmath.vector3(go.get_position().x, go.get_position().y + Speed_Mult * dt, 0))
	elseif action_id == hash("S") and not Console then
		go.set_position(vmath.vector3(go.get_position().x, go.get_position().y - Speed_Mult * dt, 0))
	elseif action_id == hash("A") and not Console then
		go.set_position(vmath.vector3(go.get_position().x - Speed_Mult * dt, go.get_position().y, 0))
	elseif action_id == hash("D") and not Console then
		go.set_position(vmath.vector3(go.get_position().x + Speed_Mult * dt, go.get_position().y, 0))
	end
end

I have looked through all the questions on this topic, but I have not found an answer.

What is this look_at function?

I got it here: Look at

function look_at(target_position, my_position, turn_object)
	local angle = math.atan2(my_position.x - target_position.x, target_position.y - my_position.y)
	go.set_rotation(vmath.quat_rotation_z(angle), turn_object)
end

It looks like you have two different game objects? And you set the position of one of them and get the position from the other. Is that really correct? What does your game object hierarchy look like?

The use of math.atan2 is wrong in the example, it should be given the Y coordinate first and then X. math.atan2() ref

g"/Player/go" this is a temporary object attached to “screen_x” and “screen_y”, I created it for debugging :slight_smile:

here’s what happens if I swap x and y: