Welcome to the forums @Henrik!
Is the second position in global space or local space? The cast_to
variable is a position relative to the center of the raycast, so you will want to take the second position, the target, and convert it to a position relative to the Raycast2D if it is not already. You can do something like this:
RayCast.cast_to = RayCast.to_local(player.position);
# or, if the above does not work something else to try:
# RayCast.cast_to = RayCast.to_local(player.global_position)
RayCast.force_raycast_update()
var colliding = RayCast.is_colliding()
Also, something else to keep in mind is that Raycast2D nodes (and really any physics nodes) will only update on every _physics_process
call. If this code is not in the _physics_process
, you may want to yield for a single physics frame so it has up to date data:
RayCast.cast_to = RayCast.to_local(player.position);
RayCast.force_raycast_update()
yield(get_tree(), “physics_frame”)
var colliding = RayCast.is_colliding()
And that should, hopefully, solve the issue :smile: