Raycasting infront, from Point A to Point B

Hi I’m currently trying to figure out, how I can create multiple parts from Part A to Part B, utilizing raycast to go over obstacles. I’m not sure where to start, for how to actually go over the part itself. I’d like the final product to look like the second image. I’m not expecting a script, but a few pointers to help me make it. Cheers.

I’m not sure I understood correctly, so I’m simply going to ask for some clarifications to also help future people that want to aid you.

You want to be a able to draw the “outline” around any obstacles in between two points A and B marked by the red neon parts?

Also, bonus question. If obstacles were to have space below them as well (imagine the two points A and B are midair), would you like the line to also be able to go below the obstacles?

“You want to be a able to draw the “outline” around any obstacles in between two points A and B marked by the red neon parts?”
I would like it to generate a series of parts connected to eachother (in order to avoid obstacles), as shown in the second image.
Conventionally, when you just do CFrame.new(part1,part2), it creates a straight line and doesn’t account for any obstacles.

“Also, bonus question. If obstacles were to have space below them as well (imagine the two points A and B are midair), would you like the line to also be able to go below the obstacles?”
Yep, my intent is that if there is air in front, it will raycast downwards and then move from there.

Unfortunately there’s not a predefined way of doing it automatically.

Also, for this to be possible you need to understand how Raycasts and 3D space work:

I’ll explain this in steps, you can think of it as an iterating algorithm, and I will be presenting the different stages of the algorithm through sketches.

Step 1: Initialization
We have two points A and B, and we have some random form obstacle between us.

Step 2: Vertical Search
What we want to do, is starting from point A, raycast continuously in both directions, with a certain step distance (d) to understand the obstacle’s shape. It’s important to note that while moving up and down, we are always hitting the same wall on the same X coordinate (marked as dotted blue in the sketch).

Step 3: Vertical Search End
At some point, as we go in both directions, we will eventually hit a wall that is no longer on the same X coordinate as the other points. These raycasts that are no longer hitting the wall on the same X are marked in orange!
When this happens, this marks the end of the Vertical Search.

Step 4: Horizontal Search
This is just a mirror of the previous Vertical Search Step.
From the last point where your ray hit a valid wall (meaning the X of the hit was on the blue line), go right the same distance (d), go up or down (depending on the upper or lower path) and start raycasting downwards or upwards (depending on the upper or lower path).
For example, in the upper path, we moved slightly to the right from the last valid ray, and then we started raycasting downwards!

At some point, same as before, you will hit a wall whose Y coordinate is different then the default Y coordinate that previous rays where hitting (the axis marked with blue). This marks the end of the Horizontal Search, and you return to Step 2: Vertical Search.

Loop

You will keep doing this, until at some point, the 2 pathways will intersect while doing the vertical search.

Step 5: Obstacle Scan End


When the two rays intersect (marked by the bold red line on the right), it means the looping algorithm is over. Now we need to extract the essential points.

Step 6: Essential Points Extraction
From each row/column of raycasts, only the first and last are truly essential points. We mark those in blue.


The rest of the intermediary raycasts are not needed, and where only used to make sure we properly detect all changes in form of the obstacle. Finally when we extract the points, we should have the following:

Step 7: Pathways Deduction
After we have the essential points, we can connect the ones from the upper and the ones from the lower pathway, to see which one is the shortest:

After checking which path is the shortest, all you need to do is deduce how to place the parts in between the essential points extracted, connecting them to form the desired output.

8 Likes

You are the nicest developer. I ever seen for telling lolbillyyy All the steps and doing drawings. I Respect what you’re doing keep going😀

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.