If you mean that you need to have an infinite ray cast, you can simulate it by casting a very long segment, you'll need to calculate a normalized direction vector first:
extends Node2D
var direction = Vector2()
var length = 0
func _physics_process(_delta):
# Simulate infinite ray cast.
# In this case, we derive the size from the viewport size.
length = get_viewport().size.length_squared()
var from = $character.global_position
var to = get_global_mouse_position()
# Returns normalized direction vector.
direction = from.direction_to(to)
# Extend the ray in the given direction.
# Also convert to global coordinates.
to = (direction * length) + from
# "Infinite" ray cast!
var space = get_world_2d().direct_space_state
var result = space.intersect_ray(from, to)
var collided = not result.empty()
print(collided)
update()
func _draw():
# For debugging purposes.
var from = $character.global_position
var to = $character.global_position + (direction * length)
draw_line(from, to, Color.green)
But bear in mind that casting long vectors like this every frame can severely impact the performance. Instead of calculating the viewport size, you can also base this on maximum level size, for instance.
Alternatively, you can also "grow" the length of such a ray cast dynamically (from zero length to level size length, with some optimal step) and do multiple queries on each increase in length, if you find this could improve performance.