draw_line working incorrectly

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

Hey!

To keep it short and sweet, I’m trying to draw a line from a Node2D to the Mouse Position. It’s working decently, but for some reason, the line does actually touch the mouse or come anywhere near it for that matter. Here is what I mean:

Example

Here is my script attached the Node2D placed directly on the orange square:

extends Node2D

func _physics_process(_delta):
	look_at(get_global_mouse_position())
	update()
	
func _draw():
	var color = Color(0, 1, 1)
	var from = self.position.normalized() * 100
	var to = get_global_mouse_position().normalized() * 100
	draw_line(from, to, color)

Thanks to anyone who can help me solve this problem!

:bust_in_silhouette: Reply From: timothybrentwood

I think this should work assuming I am understanding your node structure correctly:

extends Node2D

func _physics_process(_delta):
	update()

func _draw():
	var color = Color(0, 1, 1)
	var from = Vector2.ZERO
	var to = get_local_mouse_position()
	draw_line(from, to, color)

Perfect! Using the local mouse position fixes it perfectly, however, I don’t understand why… I’m gonna do some research and see if I can figure out what caused this.

Thanks for your help!

Halflove | 2021-11-13 15:34

No problem. The line is drawn relative to the node that script is attached to. So the origin point should just be the origin of the node, i.e. (0,0), then the end point should be where the mouse is in relation to the node, i.e. local coordinates.

timothybrentwood | 2021-11-13 17:24