I'm trying to draw line from a kinematic2d enemy to my player node.
In the enemy node _draw:
var playerPos = get_parent().get_node("Player").position
draw_line(Vector2.ZERO, playerPos, Color.red, 4, true)
See screenshot:
I understand the first argument is relative to the current node's position (where is this in the godot docs?), but I don't understand why I get the result I do in the screenshot. I gave a relative position and a position. Why is the second position considered somewhere else?

  • Okay. So what you want is the relative position from the enemy to the player. Right now you are getting the position of the player (which is in the same space as the enemy, inside YSort) however, what you want is the vector from the enemy to the player. This should be the code, or at least get you in the right direction.

    var toPlayer = get_parent().get_node("Player").position - position
    draw_line(Vector2.ZERO, toPlayer, Color.red, 4, true)

Can you show your node tree (the panel on the left of the editor). All positions are relative, and are nested, so there can be an arbitrary number of local coordinate spaces. It will also depend on where you are drawing the line, I assume from the screenshot it is within the enemy. It could also depend on how the enemies are rotated.

    Okay. So what you want is the relative position from the enemy to the player. Right now you are getting the position of the player (which is in the same space as the enemy, inside YSort) however, what you want is the vector from the enemy to the player. This should be the code, or at least get you in the right direction.

    var toPlayer = get_parent().get_node("Player").position - position
    draw_line(Vector2.ZERO, toPlayer, Color.red, 4, true)