Help with Rendercam and Cursor game object position

I am trying to use the Rendercam to allow the cursor to maintain its same position relative to the screen when zooming in and out using the camera however now I have done this the cursor game object no longer matches the position of the cursor on screen, and instead maintains a position in the bottom left corner of the screen. While I am glad that the game objects position is now relative to the screen now it doesn’t follow the cursor.

This is the code the game object which controls the zooming in and out and updating the position of the cursor:

local rendercam = require "rendercam.rendercam"
local mouse_spos = vmath.vector3()
local mouse_wpos = vmath.vector3()
local zstep = 10
local cursor = msg.url("main", "/cursor", "cursor")

function init(self)
	msg.post(".", "acquire_input_focus")
	self.mouse_spos = vmath.vector3()
end

function update(self, dt)
	self.mouse_wpos = rendercam.screen_to_world_2d(self.mouse_spos.x, self.mouse_spos.y)
	msg.post(cursor, "update pos", {pos = self.mouse_wpos})
end

function on_input(self, action_id, action)
	if action_id == hash("zoomin") then
		rendercam.zoom(-zstep)

	elseif action_id == hash("zoomout") then
		rendercam.zoom(zstep)
	end
end

function final(self)
	msg.post(".", "release_input_focus")
end

And this is the code for the cursor:

function init(self)
	self.pos = go.get_position()
end

function on_message(self, message_id, message, sender)
	if message_id == hash("update pos") then
		self.pos = message.pos
		go.set_position(self.pos)
	end
end

This is a video showing what is happening. The white orb is supposed to be at the position of the mouse cursor. The Defold logo is just for scale

What is the question?

Also, you don’t update self.mouse_spos, so I wouldn’t expect the white sprite to move.

The question is how would I get the white sprite (which is the cursor game object) to move with the position of the cursor?

The mouse cursor input has action_id = nil and you can use the action.x/action.y (or action.screen_x/action.screen_y)

adding

function on_input(self, action_id, action)
	if action.screen_x and action.screen_y then
		local pos = vmath.vector3(action.screen_x, action.screen_y, 0)
		go.set_position(pos)
	end
end

to the cursor script doesn’t change anything. What exactly should I change to my script to make it work?

Is the “cursor” object under the same game object as the script?
If not, that’s the problem.
The “go.set_position(pos)” will set position for the gameobject that the script is attached to.

Note that the “id” is optional:

Yeah, the script for the cursor is under a game object called curser which contains the script and the white orb sprite.

This is the main.collection
image

Despite this it still doesn’t work.

1 Like

I forgot to mention that this didn’t fix it and I already had it set up like this before you said.

Ok so I think I figured it out. In the ‘on input’ function in the main.script you need this line of code:

if action_id == nil then
		self.mouse_spos.x = action.screen_x;  self.mouse_spos.y = action.screen_y
end

thanks for you help anyway

2 Likes

Yes, perhaps I was unclear in my first reply.

It’s good that you got it to work.