Attention | Topic was automatically imported from the old Question2Answer platform. | |
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():
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)
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://forum.godotengine.org/40058/what-is-the-difference-between-global-and-local-coordinates#:~:text=Local%20co-ordinates%20are%20relative,including%20off-screen%20space).
So the function would be draw_line(Vector2.ZERO, to_local(body.global_position)
.
Hope this is helful
NSakamaki | 2020-12-28 19:06