draw_line to body position problem

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

Hi, i have a script where looked for a player in my area2D entered and if a player entered it must be draw a line but it doesnt work.

Here my script:

extends KinematicBody2D

export(Color) var color

onready var rr = $RR

func _process():

func _draw():
func _on_CanvasModulate_draw():

	for body in rr.get_overlapping_bodies(): #rr is my area2D
		can_draw = true
		var pos : Vector2 = body.position
		var self_pos : Vector2 = self.position
		draw_line(self_pos, pos , color, 1)

Change body.position to to_local(body.global_position) to make the positions relative to the position of the node attached to the script.
Node.position is the position relative to the parent, but draw_line actually needs to have the position relative to the node it’s called from.
Also, since the positions are relative to the current position, self_pos can actually be completely replaced with Vector2.ZERO.
There is a question about global and local coordinates, too, if you want to learn more:
So the function would be draw_line(Vector2.ZERO, to_local(body.global_position).
Hope this is helful :slight_smile:

NSakamaki | 2020-12-28 19:06