0 votes

Hello!

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 = getparent().getnode('item')

func physicsprocess(delta):
update()

func draw():
draw
line($player.position, item.globalposition, linecolor, 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!!!

in Engine by (58 points)

1 Answer

+2 votes
Best answer

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

item.global_position

with

$player.position + item.global_position
by (135 points)
selected by

ohhh you are (almost) right!

it was:

item.globalposition - $playercenter.global_position

doing this it works perfectly!

thank you very much!!!

Glad you got it sorted out!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.