Advertisement
DrawingJhon

camera raycast

Feb 27th, 2022 (edited)
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.44 KB | None | 0 0
  1. local function show(ray)
  2.     local origin = ray.Origin
  3.     local direction = ray.Direction
  4.     local position = (CFrame.new(origin) * CFrame.lookAt(Vector3.new(), direction) * CFrame.new(0, 0, -direction.magnitude)).Position
  5.     local distance = (origin - position).Magnitude
  6.     local cf = CFrame.lookAt(origin, position)
  7.     local p = Instance.new("Part")
  8.     p.Anchored = true
  9.     p.CanCollide = false
  10.     p.BrickColor = BrickColor.new("Really red")
  11.     p.Size = Vector3.new(0.05, 0.05, distance)
  12.     p.CFrame = cf*CFrame.new(0, 0, -distance/2)
  13.     p.Material = "SmoothPlastic"
  14.     p.Transparency = 0.3
  15.     p.Parent = workspace.Base
  16.     return cf.Position, (cf * CFrame.new(0, 0, -distance)).Position
  17. end
  18.  
  19. local function concat(v1, v2)
  20.     show(Ray.new(v1, CFrame.lookAt(v1, v2).lookVector * (v1 - v2).magnitude))
  21. end
  22.  
  23. local function camRay(n1, n2)
  24.     local camera = workspace.CurrentCamera
  25.     local length = 60
  26.     local unitRay = camera:ScreenPointToRay(n1, n2)
  27.     local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
  28.     return show(ray)
  29. end
  30.  
  31. local inset = game:GetService("GuiService"):GetGuiInset()
  32. local vt = workspace.CurrentCamera.ViewportSize
  33. local min0, max0 = camRay(-inset.X, -inset.Y)
  34. local min1, max1 = camRay(vt.x, -inset.Y)
  35. local min2, max2 = camRay(vt.x, vt.y)
  36. local min3, max3 = camRay(-inset.X, vt.y)
  37. concat(min0, min1)
  38. concat(min1, min2)
  39. concat(min2, min3)
  40. concat(min3, min0)
  41. concat(max0, max1)
  42. concat(max1, max2)
  43. concat(max2, max3)
  44. concat(max3, max0)
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement