I imagine this is likely an easy case, as I’m sure raycasting is used for this often.
How can I raycast, from one part, to another part (not from one part, in a direction), and check if the ray is obstructed before it reaches the other part?
Preferably keep it simple, and have a little bit of explanation too; I don’t like blindly copying code, I like to understand what I’m doing and learn from it.
So the raycast function takes a Position (Origin) and a Direction, so you actually just need to create a direction vector between 2 points
The code for that is simply
local Origin = Part1.Position
local Target = Part2.Position
local Result = workspace:Raycast(Origin, Target-Origin)
If you want to learn specifically how this works then its just mathematics and you can read more below
So the 2 circles are both part positions, and remember that positions are represented by vector3’s and in drawing they are represented by arrows… So im drawing an arrow from the origin of the world (0,0,0) to where the positions are
Now in this formula Target - Origin, you should think of it as Target + (-Origin), the - means to inverse the vector so if I inverse the origin vector it will look like this
(not a perfect drawing)
the dark blue line represents the inverse of the origin vector, now we just add them together
To add a vector I am simply moving the dark blue vector onto the “tail” of the red vector then you simply draw the final vector from the origin
This green vector is the result vector from Target + (-Origin), and you can see its the exact vector we need if we move it up some