Advertisement
StefanBashkir

GetDirection of wedged face

Sep 29th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. --returns unit vector n, the normal of the face of Part `part` which contains Vector3 `pos`
  2. local function GetNormal(part, pos)
  3.     local abs = math.abs
  4.     local shape = part:IsA("Part") and part.Shape.Value or part:IsA("WedgePart") and 3 or part:IsA("CornerWedgePart") and 4
  5.     if shape == 0 or shape == 2 then
  6.         return (pos-part.Position).unit
  7.     elseif shape == 1 or shape == 3 then
  8.         local vec = part.CFrame:pointToObjectSpace(pos)/part.Size
  9.         local rot = part.CFrame-part.Position
  10.         local x, y, z = vec.X, vec.Y, vec.Z
  11.         if x > 0.499 then return rot * Vector3.new(1, 0, 0)
  12.         elseif x < -0.499 then return rot * Vector3.new(-1, 0, 0)
  13.         elseif shape == 1 and y > 0.499 then return rot * Vector3.new(0, 1, 0)
  14.         elseif y < -0.499 then return rot * Vector3.new(0, -1, 0)
  15.         elseif z > 0.499 then return rot * Vector3.new(0, 0, 1)
  16.         elseif shape == 1 and z < 0.499 then return rot * Vector3.new(0, 0, -1)
  17.         end
  18.         return rot * Vector3.new(0, part.Size.Z, -part.Size.Y).unit
  19.     elseif shape == 4 then
  20.         local vec = part.CFrame:pointToObjectSpace(pos)/part.Size
  21.         local rot = part.CFrame-part.Position
  22.         local x, y, z = vec.X, vec.Y, vec.Z
  23.         if x > 0.499 then return rot * Vector3.new(1, 0, 0)
  24.         elseif y < -0.499 then return rot * Vector3.new(0, -1, 0)
  25.         elseif z < -0.499 then return rot * Vector3.new(0, 0, -1)
  26.         elseif vec.unit:Dot(Vector3.new(0.707106769, 0, 0.707106769)) > 0 then return rot * Vector3.new(0, part.Size.Z, part.Size.Y).unit
  27.         end
  28.         return rot * Vector3.new(-part.Size.Y, part.Size.X, 0).unit
  29.     end
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement