I have a node which has a child KinematicBody2D
as such:
and the script of the Holder
as such
extends Node2D
onready var item = $Item
func _physics_process(delta):
global_position.y += 3000 * delta
func _draw():
draw_line(
to_local(item.global_position) - Vector2(100, 0),
to_local(item.global_position) + Vector2(100, 0),
Color.violet
)
In theory, the draw_line()
should draw at the position of the Item
(green) but I'm guessing since sync_to_physics
is enabled there is some offset due to catching up?
Regardless, the problem arises that the global_position
being returned is wrong due to which the line being drawn is at the wrong position:
What's more bizarre is that when I resize the window it corrects itself:
So is there a reliable way / workaround I can get the global_position
of a KinematicBody2D
with sync_to_physics enabled?
Edit: A continuation of the question has been asked here
You are getting the position correctly, and sync to physics is not the issue at hand either.
What happens is that Godot avoids calling _draw
unless it knows it need to redraw. So once your _draw
function ran, Godot won't call it again unless something triggers it (resize the window, for example).
You can tell Godot to call _draw
again by calling queue_redraw()
in Godot 4, or update
in Godot 3. For example:
extends Node2D
onready var item = $Item
func _physics_process(delta):
global_position.y += 3000 * delta
update()
func _draw():
draw_line(
to_local(item.global_position) - Vector2(100, 0),
to_local(item.global_position) + Vector2(100, 0),
Color.violet
)