Raycast Loop for Each Frame?

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

So I am trying to collect a series of points that update each frame. Below is the a code snippet.

 public override void _PhysicsProcess(float delta)
{
 var rRayCastSkull = GetWorld().DirectSpaceState;

    Godot.Collections.Array rSkullHitObjects = new Godot.Collections.Array();

    //declare list of new raycast spawn points
    List<Vector3> rSkullSpawnPoints = new List<Vector3>() { soundSource.GlobalTransform.origin };        

    for (int i = 0; i < rSkullSpawnPoints.Count; i++)
    {
        var rRaycastSkullResult = rRayCastSkull.IntersectRay(rSkullSpawnPoints.Last(), earDrumRight.GlobalTransform.origin, rSkullHitObjects, 4);

        if (rRaycastSkullResult.Keys.Count > 0 && rRaycastSkullResult["collider"] != null)
       
            rSkullSpawnPoints.Add((Vector3)rRaycastSkullResult["position"]);

            GD.Print("rSkullHitPoint:  " + (Vector3)rRaycastSkullResult["position"]);               
        
    }
}

Every time a raycast collides with an object, the collision point is added to the list of hitpoints. I am then trying to use a for loop to initialize a new raycast starting at the hitpoint of the last raycast. This allows the user to collect collider information going through a complex object.

I’m having some trouble getting this loop working. It’s probably too much happening for one frame. Any ideas for alternative solutions?

I apologize for the messy code. Thanks in advance!

What results are you getting? Have you tried stepping through the code while it’s running?

Only thing I could think of if I understood the intent of your code right is that you start new ray from where the last ray hit. The new ray may be hitting the very same collider, not advancing a single bit thus resulting in infinite loop. Try adding a small offset to new point (position + ray direction * 0.001).

aXu_AP | 2022-09-09 19:59