0 votes

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():
        update()

func _draw():
    _on_CanvasModulate_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)
Godot version 3.2.3
in Engine by (85 points)

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:
https://godotengine.org/qa/40058/what-is-the-difference-between-global-and-local-coordinates#:~:text=Local%20co%2Dordinates%20are%20relative,including%20off%2Dscreen%20space).
So the function would be draw_line(Vector2.ZERO, to_local(body.global_position).
Hope this is helful :)

Please log in or register to answer this question.

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.