draw a line between two nodes

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


I have my game structured like this:

  • level
    • item (position2D)
    • player (position2D)

I have a script in player and I want to draw a line between item and player, the line should update as the player moves (item will be static), like they were tied by a rope.

This is the script in player:

onready var item = get_parent().get_node(‘item’)

func _physics_process(delta):

func _draw():
draw_line($player.position, item.global_position, line_color, line_width)

The problem is that the line gets drawn from the player (correct) but instead of going to the point where is the item (in this case in point (200, 100)) it gets drawn just 200 pixels to the right and 100 down where the player is.

What should I do???

Thank you!!!

:bust_in_silhouette: Reply From: Ben Humphries

I think the problem here is that you’re using the global position of the item, whereas you need the position relative to the player. Without testing it myself, I think you could replace



$player.position + item.global_position

ohhh you are (almost) right!

it was:

item.global_position - $player_center.global_position

doing this it works perfectly!

thank you very much!!!

hermo | 2020-06-23 15:21

Glad you got it sorted out!

Ben Humphries | 2020-06-23 15:24