How to check if parts have something between them

The topic name is pretty self-explanatory.
I tried using workspace:Raycast, but it ended up making false detections or not working at all.

Could you provide your raycasting code? The ray may be casting in the wrong direction.

I was able to write up a script that checks if the direct path between two parts is obscured:
image
image

Here’s the code:

local RS = game:GetService("RunService")

local part1, part2 = workspace.Part1, workspace.Part2

local params = RaycastParams.new()
params.FilterDescendantsInstances = {part1, part2}

local att0 = Instance.new("Attachment")
att0.Parent = part1
local att1 = Instance.new("Attachment")
att1.Parent = workspace.Terrain

local beam = Instance.new("Beam")
beam.Attachment0 = att0
beam.Attachment1 = att1
beam.Parent = workspace.Terrain

RS.Heartbeat:Connect(function()
	local origin = part1.Position
	local destination = part2.Position
	
	local direction = destination - origin
	
	local result = workspace:Raycast(origin, direction, params)
	if result then
		--// SOMETHING IS IN THE WAY
		att1.WorldPosition = result.Position
	else
		--// Nothing is in the way! :)
		att1.WorldPosition = destination
	end
end)

Whenever you’re casting from point A to point B you will want the direction to be B - A. Otherwise your ray will cast in the opposite direction. You can find a picture explaining it in this older post.

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