Actual node position different to the vector I'm drawing it on

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By arty3000

I have the below script attached to a Node2d object

func _process(delta):

func _draw():
	position = get_viewport().get_mouse_position()
	draw_circle(position, 10, Color("#ff0000"))
	print("circle position: " + str(self.position))

This script should simply draw a red circle on top of the mouse.

However, when I run the app the circle always 50ish pixels to the bottom right of the mouse position as the image shows. The Node the script is attached to is a child of the root node.

I don’t understand why?


:bust_in_silhouette: Reply From: Zylann

position is a property of Node2D. You assigned the mouse position to it, so your node is moved under the mouse.
But then you draw a circle at position, which is relative to the node’s position, so… it draws always offset (global result is then position + position).

You may want to try this instead:

var mouse_pos = get_viewport().get_mouse_position()
draw_circle(mouse_pos, 10, Color("#ff0000"))

Or this

position = get_viewport().get_mouse_position()
draw_circle(Vector2(0, 0), 10, Color("#ff0000"))