Raycast2D not colliding with instanced areas2D

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

Hello all,

I have a problem with my raycast2D to get the collision with an area2D.

Im instancing the scenario and its obstacles randomly on my _ready() function, and i need to check from a random generate path of raycasts2D if any of the obstacles are colliding with the path.

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")

This is a code im using in a test scene, where im instancing in my ready() func an obstacle, and then i need to check if the raycast2D collide. It should be, but is not working.

If i put the obstacle directly on the scene (no by code) it works and collide, if i use _process(delta) func, it works and collide… But how the hell can i make it work this way?? (If its possible)

I really must do all this in my _ready() func, because i need to generate all before the game can start, so, if its not possible to do it with raycast2D i must find another way to achieve this.

Thanks in advance

:bust_in_silhouette: Reply From: Uggeli

You need to check that raycast has enabled colliding with areas.

$RayCast2D.set_collide_with_areas(true)

or from editor under raycast collide with tab