Raycasting Assistance

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.

There’s already a great solution (by royaltoe) to your question on the following post:

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

image

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
image
(not a perfect drawing)
the dark blue line represents the inverse of the origin vector, now we just add them together
image
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
image
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
image

couldnt i just raycast from part1 to part2 instead of all that

That’s what they explained. The pictures are showing you the vector math.

Also note that you’ll want to add the two parts to an ignore list in the params.

Roblox made an article here:
https://developer.roblox.com/en-us/articles/Raycasting

It explains everything you’ll need to know.

That is what I explained yes, if you’re asking if you can just do

:Raycast(Part1.Position,Part2.Position),

then no because doing that would result in the first image