Help with raycasting through a whole part

Hi everyone! I’m currently trying to cast a ray through a whole part but I’m having some issues. When I rotate the part to touch an object the ray doesn’t seem to detect it. Without rotation, it works fine. If anyone could spot any mathematical errors in my code that would be appreciated!

while true do
wait(0.5)
local Part = script.Parent
local LCF = Part.CFrame - Vector3.new(0, 0, Part.Size.Z/2) --Left bound
local RCF = Part.CFrame + Vector3.new(0, 0, Part.Size.Z/2) --Right bound
local LV = Vector3.new(LCF.x,LCF.y,LCF.z)
local RV = Vector3.new(RCF.x,RCF.y,RCF.z)
local ray = Ray.new(LV,(RV-LV))
local r = workspace:FindPartOnRay(ray,script.Parent)
print(r)
end

If I’m understanding your code correctly, is lines 4-7 trying to find the center of the part? If you’re simply trying to raycast from the center of the part, you can simplify those steps to just getting the part’s position.

The 2nd argument of the ray construction takes in a vector3 as a direction. So if you want to cast a ray forward, you would use Part.CFrame.LookVector, and then multiple by how many studs you want to go out.

So the final ray will be constructed like this:
local ray = Ray.new(Part.Position, Part.CFrame.LookVector * 100)

I think I may be misunderstanding something but hopefully this points you in the right direction.

1 Like