Advertisement
Silverlan

Untitled

Feb 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.52 KB | None | 0 0
  1. local function SquaredDistance(a,b)
  2.     return (a -b):LengthSqr()
  3. end
  4.  
  5. local function FindClosest(ent,tEnts)
  6.     local pos = ent:GetPos()
  7.     local dClosest = math.huge
  8.     local entClosest
  9.     for _,entTgt in ipairs(tEnts) do
  10.         local posTgt = entTgt:GetPos()
  11.         local dSqr = SquaredDistance(pos,posTgt)
  12.         if(dSqr < dClosest) then
  13.             dClosest = dSqr
  14.             entClosest = entTgt
  15.         end
  16.     end
  17.     local dist = math.sqrt(dClosest) // ACTUAL DISTANCE
  18.     return entClosest,dist
  19. end
  20. local entClosest = FindClosest(player.GetAll()[1],ents.GetAll())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement