Raycast doesnt collide with a tilemap

Godot Version

4.6

Question

a raycast is not colliding with a tilemap even though it is going right through. the tilemap is on the same collision mask as the raycast. it managed to collide before(i have actually posted a seperate topic about this exact same raycast and it is an entirely seperate issue)

here is a screenshot of the scene running

here is a screenshot of the scene tree and inspector

and text version of the code

extends Area2D
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
@onready var ray_cast_2d: RayCast2D = $RayCast2D

@export var maximumLength = 5000

func calculate_distance(vector1, vector2):
var distanceVector = abs(vector2 - vector1)
return sqrt(distanceVector.x ** 2 + distanceVector.y ** 2)

Called when the node enters the scene tree for the first time.

func _ready() → void:
ray_cast_2d.target_position = Vector2(50000,0)
print(ray_cast_2d.position, ray_cast_2d.global_position, ray_cast_2d.target_position)
ray_cast_2d.force_raycast_update()
ray_cast_2d.add_exception(self)
var position = ray_cast_2d.get_collision_point()
print(ray_cast_2d.is_colliding())
print(ray_cast_2d.get_collider())
print(ray_cast_2d)
print(ray_cast_2d.collide_with_bodies)
print(ray_cast_2d.enabled)
print(position)
collision_shape_2d.shape.b = position

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
pass

game files if really desperate:

sorry to leave you with the file , i know its a bad thing to do but im not gonna available cause im posting this right before sleeping and am only going to be able to answer questions at most after 10 minutes or so

Check in _physics_process()

Is that red line supposed to be the ray cast line? The debug print shows that the ray cast’s global position is (0,0) and the target position relative to the ray cast position is (50000, 0). The red line doesn’t seem to match that, unless there are rotations or something else going on.

i rotated it slightly

it works in physics process, but is there any way to get it in ready?

Why do you need it in _ready()?

Try waiting a frame before making the ray cast:

await get_tree().process_frame

But these kind of waits always feel a little bit bad. _physics_process would be better.

easier to program what i was making
anyway gonna be marking this as solution so:
the issue was due to me trying to use the raycast before the scene had properly rendered, so it can be solved by making the program wait until the scene is rendered, or as many suggested , to put the check into physics process.