Advertisement
StefanBashkir

Find all parts on a ray

Mar 24th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.89 KB | None | 0 0
  1. function RecurseRaycast(qRay, IgnoreList) -- IgnoreList is a table of objects to ignore.
  2.     local Returns = {};
  3.     local OriginalLength = qRay.Direction.magnitude;
  4.     local Raycast;
  5.     function Raycast(qRay, IgnoreList)
  6.         local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(qRay, IgnoreList);
  7.         if (Hit) then
  8.             table.insert(Returns, {Hit,Pos,Normal});
  9.             table.insert(IgnoreList, Hit);
  10.             local NewRay = Ray.new(qRay.Origin, qRay.Direction.unit * (OriginalLength - (qRay.Origin - Pos).magnitude ))
  11.             return Raycast(NewRay, IgnoreList)
  12.         end
  13.     end
  14.     Raycast(qRay, IgnoreList);
  15.     return Returns;
  16. end
  17.  
  18. -- Example of usage
  19.  
  20. local Returns = RecurseRaycast(
  21.     Ray.new(workspace.RayTester.Position, workspace.RayTester.CFrame.lookVector * 500),
  22.     {workspace.RayTester}
  23. )
  24.  
  25. if (#Returns == 0) then
  26.     print("Ray hit nothing.");
  27. else
  28.     for i,v in ipairs(Returns) do
  29.         print(unpack(v))
  30.     end
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement