How do I sync the duplication with the cursor

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SwordRunning
if Input.is_key_pressed(KEY_1):
	var Item = get_node("Icon")
	var ItemD = Item.duplicate()
	ItemD.position = get_viewport().get_mouse_position()
	add_child(ItemD)

I am trying to duplicate something on the icon but it appears really off.

Have you tried get_global_mouse_position() or checked anchors, margins and offset of the icon?

dewcked | 2021-11-22 08:22

also You should try applying change to global_position instead of position

Inces | 2021-11-22 15:33

They all seem fine and get_global_mouse_position() changed nothing.

SwordRunning | 2021-11-22 20:20

same thing happened with global_position

SwordRunning | 2021-11-22 20:21

Also you checked Every ancestor nodes’ anchors, margins, rect_position, position?

dewcked | 2021-11-23 05:21

:bust_in_silhouette: Reply From: Inces

Don’t You have any code in “Icon” ready() function ?
What do You mean “off” ? Show print screen of that. Perhaps it is just icon is large and its origin point is corner ?

Tried it with a different image and got the same resultI have code that constantly checks for input and if the input is detected it duplicates the sprite.

Imgur: The magic of the Internet

SwordRunning | 2021-11-22 22:08

Are there no errors in console ?
Is resulting off-position the same when project is window and fullscreen ?

Inces | 2021-11-23 13:35

Same results in fullscreen and no errors in console.

SwordRunning | 2021-11-23 19:43

Let’s see if this has anything to do with duplication.
Don’t duplicate, create icon anew. From Your script I can see this is a Sprite ?

var itemd = Sprite.new()
itemd.global_position = get_mouse_global_position()
add_child(itemd)

This should create icon so its top left corner is at cursor

Inces | 2021-11-23 20:31