Hello! I’ve been trying to design a script that auto calculates the distance between a part and another part in the direction the casting part is facing. I just don’t know where to start… I’ve tried Ray.new casting from the Cframe.LookVector, but with no luck.
It’s really hard because if I want to rotate the part, I need to ensure it casts forwards and isn’t based on a world rotation. Any pointers would be great! (Pun intended)
local part1 = script.Parent
local function GetPartsInFrontRay(start)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {start}
params.FilterType = Enum.RaycastFilterType.Blacklist
local vector = start.CFrame.LookVector
local ray = workspace:Raycast(start.Position, vector * 40, params)
if ray and ray.Instance then
return ray.Instance
end
end
while task.wait(.05) do
local part = GetPartsInFrontRay(part1)
if part then
warn(part.Name)
end
end
Oh, that’s how you tie in the look vector!
Incredible, thank you
Although I may be dumb, how would I calculate the distance between the hit part and the start position using this method?
Edit:
Nevermind! You can do: origin.Position.Magnitude - part.Position.Magnitude
To get the distance between the two parts