Advertisement
AlewAlow

raycasting

Apr 15th, 2024
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local UserInputService = game:GetService("UserInputService")
  3.  
  4. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  5. local Components = require(ReplicatedStorage.Shared.Components)
  6. local Constants = require(ReplicatedStorage.Shared.Constants)
  7.  
  8. local GetEntityGlobalScale = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalScale)
  9. local GetEntityGlobalPosition = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalPosition)
  10. local GetEntityDescendants = require(ReplicatedStorage.Shared.Utils.GetEntityDescendants)
  11.  
  12. local function FireRaycast(origin, direction, entities, world)
  13.     if type(entities) == "number" then
  14.         entities = GetEntityDescendants(entities, world)
  15.     end
  16.  
  17.     local closestHitDistance = math.huge
  18.     local closestHitNormal = nil
  19.     local closestHitPosition = nil
  20.     local closestHit = nil
  21.  
  22.     for _, id in entities do
  23.         local transform = world:get(id, Components.Transform)
  24.         if not transform then
  25.             continue
  26.         end
  27.  
  28.         local collider = world:get(id, Components.Collider)
  29.         if not collider then
  30.             continue
  31.         end
  32.  
  33.         local globalSize = GetEntityGlobalScale(id, world) * collider.Size
  34.         local globalPosition = GetEntityGlobalPosition(id, world)
  35.  
  36.         local tp = globalPosition - globalSize / 2
  37.         local br = globalPosition + globalSize / 2
  38.        
  39.         local t1 = (tp - origin) / direction
  40.         local t2 = (br - origin) / direction
  41.        
  42.         local entryTimeX = math.max(math.min(t1.X, t2.X), 0)
  43.         local exitTimeX = math.min(math.max(t1.X, t2.X), 1)
  44.         local entryTimeY = math.max(math.min(t1.Y, t2.Y), 0)
  45.         local exitTimeY = math.min(math.max(t1.Y, t2.Y), 1)
  46.        
  47.         if entryTimeX > exitTimeY or entryTimeY > exitTimeX then
  48.             continue
  49.         end
  50.        
  51.         local hitDistance = math.max(entryTimeX, entryTimeY)
  52.         if hitDistance >= closestHitDistance then
  53.             continue
  54.         end
  55.        
  56.         closestHitPosition = origin + direction * hitDistance
  57.         closestHitDistance = hitDistance
  58.         closestHit = id
  59.  
  60.         if entryTimeX > entryTimeY then
  61.             if t1.X < t2.X then
  62.                 closestHitNormal = Vector2.new(-1, 0)
  63.             else
  64.                 closestHitNormal = Vector2.new(1, 0)
  65.             end
  66.         else
  67.             if t1.Y < t2.Y then
  68.                 closestHitNormal = Vector2.new(0, -1)
  69.             else
  70.                 closestHitNormal = Vector2.new(0, 1)
  71.             end
  72.         end
  73.     end
  74.  
  75.     if closestHitNormal then
  76.         return {
  77.             Position = closestHitPosition,
  78.             Normal = closestHitNormal,
  79.             Hit = closestHit,
  80.         }
  81.     end
  82.    
  83.     return false
  84. end
  85.  
  86.  
  87. return FireRaycast
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement