How to check if there is a part between 2 parts using rays?

Hello, I’m trying to make a script that knows if there is a part between 2 parts, but I don’t know how, I’ve tried doing workspace:RayCast(Part1.Position, Part2.Position) but it doesn’t return anything. I can’t find anything on the internet either. Please help

Make sure you remember the format for raycasting.

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist --Filter Type
local RayResult = workspace:Raycast(Part1.Position,(Part2.Position - Part1.Position), RayParams) --Origin, Destination, RayParams 
if RayResult then
 local RayHitPart = RayResult.Instance
end

There are a few ways you can do it, but I like this way best.

2 Likes
local Origin = Part1.Position
local Direction = CFrame.new(Part1.Position, Part2.Position).Unit
local NewRay = Ray.new(Origin, Direction  * (Part1.Position - Part2.Position).Magnitude
 )

local HitParts = workspace:FindPartOnRay(NewRay)

Look at the api for FindPartOnRay/FindPartOnRayWithIgnorelist on the wiki to figure out how to use the ignore list. There are a couple ways to do it.

1 Like

Make sure both parts are in the blacklist, and rays don’t have an Enpoint parameter, they have a Direction parameter.

workspace:RayCast(Origin, Direction, Params)

So, you have to get the direction, which you just subtract the endpoint(Part2) by the origin(Part1)

local RaycastResult = workspace:RayCast(Part1.Position, Part2.Position - Part1.Position, Params)
1 Like
The way you should do this is this:

local whitelistedParts = {
    workspace.PartInBetween;
}

local part0 = workspace.Part0
local part1 = workspace.Part1

--i subtract the two vectors because traditionally it would look like (part1.Position - part0.Position).Unit * (part1.Position - part0.Position).Magnitude which is just part1.Position - part0.Position

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = whitelistedParts

local ray = workspace:Raycast(part0.Position, part1.Position - part0.Position, params)
if ray and ray.Instance then
    print("part is in between")
else
    print("part is not between"
end
1 Like

They’re trying to get the part in between the 2 parts, how would you know the part in between the parts when we didn’t figure it out yet?

If ray is nil, this part would error. No need to check for the Instance, if there is a ray there is an Instance.


What would you need to use the unit distance for? If you were to use that, the ray might be too long or too short, so it might return a part that’s not in between the 2 parts:

4 Likes

He’s saying he wants to find if a certain part is in between two parts. If he doesn’t know the part, then he should use a blacklist instead containing both part0 and part1. And to answer your last question, although your right that you dont need to check if the ray.Instance exists if the ray already does, it won’t error - and if statement checks the first condition before moving on to the next.

1 Like

What would you need to use the unit distance for? If you were to use that, the ray might be too long or too short, so it might return a part that’s not in between the 2 parts:

I guess that’s true, but even if the ray is too long, you can just whitelist it to only search for a specific part. That’s the benefit of the whitelist.

RayParams.FilterDescendantsInstances = {Specific Part} 
RayParams.FilterType = Enum.RaycastFilterType.Whitelist

Now it’s all good. Easily making it ignore everything that it’d normally detect beside the second part.

Actually, that’s my fault. I didn’t read the question properly. I thought he was just trying to do a basic Raycast to get a distance between two parts. Seriously that’s my bad. I’ll edit that now