How to use raycast in _ready()?

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

I am creating procedural shapes, so they are empty scene with a script that creates all the hierarchy when the scene is instanced because all is triggered in ready()

the issue is, I would like to raycast from random points to the mesh to place other nodes on the surface of my geometry.

but I can’t find a way to use raycasting in the _ready() function, if I instance a raycast or use GetWorld().IntersectRay(), it won’t return anything if triggered from ready()

It does work from _PhysicProcess(), but I would like to not have to spread the generation on different frames

EDIT: I found that it works perfectly with standard physic bodies, my issue come from the fact that I am using CSG shapes (that does respond to other physic interactions) which are a bit more touchy for the physic server;

It works when I replace the CSG combiner with a static body

You could probably use a RayCast Node instead and use force_raycast_update() to get it work in _ready().

DiMiMi | 2020-10-21 12:16

:bust_in_silhouette: Reply From: rustyStriker

You can use (for 3d):

var phy = PhysicsServer.space_get_direct_state(get_world().space)
var ray = phy.intersect_ray(from, to, exclude = [], collision_mask = 1)

might as well check the docs for this function, but for me it worked in _ready()

EDIT: Didn’t see it was marked as C# but i will keep it here in case anyone else will make use of it

thanks I already tried that, my code was

PhysicsDirectSpaceState space = PhysicsServer.SpaceGetDirectState(GetWorld().Space);
Godot.Collections.Dictionary collision = space.IntersectRay(new Vector3(0, 50, 0), new Vector3(0, -50, 0));

it does work in the _ready() function for collisions with static body nodes but not CSG shapes

MajorBarnulf | 2020-10-22 19:15