function onRayHit()
-- checking for the nearest element
for name,i in pairs(ElmPos) do -- elementPositions
local currdist = (i - cam.CFrame.Position).Magnitude -- current distance
if currdist < mindist then
mindist = currdist -- assigning the minimum distance
minpos = i -- assinging the minimum position
elementName = name
end
end
-- making the fearline
FearLine = workspace:Raycast(cam.CFrame.Position, minpos - cam.CFrame.Position,whitelist)
if FearLine.Instance:FindFirstAncestor("Elements") == ElmFold then -- checking if its an element
print("seen by: " , elementName , mindist)
cube.Position = FearLine.Instance.Position
else
print("hit a wall")
-- reset distance and position
mindist = math.huge
minpos = Vector3.zero
end
end
took me a couple of days to make this search mechanic , turns out it dosent work if the shortest element is blocked by a wall even though theres a second shortest that isnt blocked , now i have to not only implement a nearest algorithm but also one that has a valid nieghbor (not hit by wall) , which sounds like a complete nightmare fuel
before you say this is shit code remember that blood and tears were put into it , im trying i really am