Problem using Raycast2d on _ready() func

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Turi

Hello all,

Im stuck trying to figure out what im missing, since i still don´t understand fully the use of raycasts2D.

Im generating all my scenario elements in my _ready() func, before the player can do anything, but when i place obstacles, i need to know if those elements are blocking a path i generate also for the scenario. All procedural.

My problem comes when i discovered that my raycast2D is not colliding with my obstacles when i instance them (If they are there since the beggining of the scene without creating them by code all is right).

Here is the code of a test scene im using to find a way to get that desired collision.

extends Node2D

var verticalSpike = load("res://scenarios/obstacles/verticalSpikeWall.tscn")

func _ready():
	verticalSpike = verticalSpike.instance()
	$spawn.add_child(verticalSpike)
	verticalSpike.position = Vector2(800,500)

	$RayCast2D.force_raycast_update()
	if $RayCast2D.is_colliding():
		print("collision")
	else:
		print("no collision")

As you see there, im instancing an scene of an obstacle that have an area2D to collide with. The raycast is passing through it but not colliding.

And yes, they have the same mask, raycast is enabled too. If i use _process(delta) the raycast collides, but i need to check in _ready() but im not finding the way to achieve this (if this is possible, if not i should find another way to check if an obstacle is blocking the path for the scenario)

:bust_in_silhouette: Reply From: vnmk8

maybe instead of var and load use onready var and preload, maybe the object is being instanced after the raycast check? just a thought.