# Help with Rendercam and Cursor game object position

**URL:** <https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098>\
**Category:** Questions\
**Created:** [August 17, 2021, 12:35pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098 "2021-08-17T12:35:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 17, 2021, 12:35pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/1 "2021-08-17T12:35:22Z")

</div>

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:

```lua
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:

```lua
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

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [August 17, 2021, 12:44pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/2 "2021-08-17T12:44:30Z")

</div>

What is the question?

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

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 17, 2021, 12:49pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/3 "2021-08-17T12:49:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [August 17, 2021, 12:54pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/4 "2021-08-17T12:54:08Z")

</div>

The mouse cursor input has action\_id = nil and you can use the `action.x`/`action.y` (or action.screen\_x/action.screen\_y)

> **[Mouse movement - Mouse and touch input in Defold](https://defold.com/manuals/input-mouse-and-touch/#mouse-movement)**
>
> It is recommended that you familiarize yourself with the general way in which input works in Defold, how to receive input and in which order input is received in your script files. Learn more about the input system in the Input Overview manual. |...

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 17, 2021, 2:18pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/5 "2021-08-17T14:18:37Z")

</div>

adding

```lua
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?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [August 17, 2021, 2:41pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/6 "2021-08-17T14:41:51Z")

</div>

> [@Frankie\_Freeland](#):
>
> `go.set_position(pos)`

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:

> **[API reference (Game object)](https://defold.com/ref/stable/go/?q=set_position#go.set_position:position-%5Bid%5D)**
>
> Defold is a free and open game engine used for development of console, desktop, mobile and web games.

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 17, 2021, 3:11pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/7 "2021-08-17T15:11:39Z")

</div>

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](https://forum-defold.b-cdn.net/uploads/default/original/3X/8/f/8ff2d93a4c973b85861312f1f6c6cd6120bc9795.png)

Despite this it still doesn’t work.

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 17, 2021, 3:37pm UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/8 "2021-08-17T15:37:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [August 18, 2021, 10:37am UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/9 "2021-08-18T10:37:34Z")

</div>

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

```lua
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

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [August 18, 2021, 11:28am UTC](https://forum.defold.com/t/help-with-rendercam-and-cursor-game-object-position/69098/10 "2021-08-18T11:28:19Z")

</div>

Yes, perhaps I was unclear in my first reply.

It’s good that you got it to work.
