Advertisement
StefanBashkir

Surface normal perpendicular direction

Feb 13th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. local M = game.Players.LocalPlayer:GetMouse();
  2.  
  3. function Part(Orientation)
  4.     local P = Instance.new("Part", workspace);
  5.     P.Anchored = true;
  6.     P.CFrame = Orientation;
  7.     P = nil;
  8. end
  9.  
  10. M.Button1Down:connect(function()
  11.     if (M.Target) then
  12.         local Surface = M.TargetSurface;
  13.         local HitPosition = M.Hit.p
  14.         local WorldSpaceNormalIdVector = Vector3.FromNormalId(Surface);
  15.         local ObjectSpaceSurfaceDirection = M.Target.CFrame:vectorToWorldSpace(WorldSpaceNormalIdVector);
  16.         -- ObjectSpaceSurfaceDirection is the direction the target surface is facing
  17.        
  18.         local Up = Vector3.new(0,1,0);
  19.         if (math.abs(ObjectSpaceSurfaceDirection.Y) >= .99) then
  20.             Up = M.Target.CFrame:vectorToWorldSpace(Vector3.new(1,0,0));
  21.             print("Target surface faces world-space 'up.' Switching world-space 'up.'");
  22.         end
  23.        
  24.         local PerpDirection = ObjectSpaceSurfaceDirection:Cross(Up);
  25.  
  26.         -- Create manual CFrame for part to "lay" on the surface.
  27.         local FrontAxis = ObjectSpaceSurfaceDirection:Cross(PerpDirection);
  28.         local Orientation = CFrame.new(HitPosition.X, HitPosition.Y, HitPosition.Z,
  29.             PerpDirection.X, ObjectSpaceSurfaceDirection.X, -FrontAxis.X,
  30.             PerpDirection.Y, ObjectSpaceSurfaceDirection.Y, -FrontAxis.Y,
  31.             PerpDirection.Z, ObjectSpaceSurfaceDirection.Z, -FrontAxis.Z
  32.         );
  33.        
  34.         Part(Orientation);
  35.     end
  36. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement