Guest User

Untitled

a guest
Jul 21st, 2018
7,836
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 1 0
  1. --//Camera Script//--
  2. --Services
  3. local UIS = game:GetService("UserInputService")
  4. local RunService = game:GetService("RunService")
  5. local TweenService = game:GetService("TweenService")
  6.  
  7. --Player
  8. local Player = game.Players.LocalPlayer
  9. repeat wait() until Player.Character
  10. repeat wait() until Player.Character.Humanoid
  11. repeat wait() until Player.Character.HumanoidRootPart
  12. local Character = Player.Character
  13. local HumanoidRootPart = Character.HumanoidRootPart
  14. local Humanoid = Character.Humanoid
  15. local CamScript = Player.PlayerScripts.CameraScript
  16. CamScript.Disabled = true
  17. Humanoid.AutoRotate = false
  18.  
  19. --Mouse
  20. local Mouse = Player:GetMouse()
  21.  
  22. --Variables
  23. local Popper = true     --  Sets whether Popper Cam behaviour is enabled
  24. local DeltaX = 0
  25. local DeltaY = 0
  26. local AngleH = 0
  27. local AngleV = 0
  28. local SensitivityY = 120    --  Determines how large a change in vertical angle is through a mouse rotation
  29. local SensitivityX = 120    --  Determines how large a change in the horizontal angle is through a mouse rotation
  30. local W = false
  31. local A = false
  32. local S = false
  33. local D = false
  34. local Offset = CFrame.new(2, 3, 15) --  Determines the CFrame by which the camera is pushed from the CFrame of the HumanoidRootPart
  35. local MaxY = 5*math.pi/12               --  Determines maximum vertical angle
  36. local MinY = -5*math.pi/12          --  Determines minimum vertical angle
  37. local Combinations = {
  38.     {true, false, false, false, 0, 0},
  39.     {true, true, false, false, math.pi/4},
  40.     {false, true, false, false, math.pi/2},
  41.     {false, true, true, false, 3*math.pi/4},
  42.     {false, false, true, false, math.pi},
  43.     {false, false, true, true, 5*math.pi/4},
  44.     {false, false, false, true, 3*math.pi/2},
  45.     {true, false, false, true, 7*math.pi/4}
  46. }
  47.  
  48. --Camera
  49. local Cam = game.Workspace.CurrentCamera
  50. Cam.CameraType = Enum.CameraType.Scriptable
  51. UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
  52. local FakeCam = Cam:Clone()
  53.  
  54. --Functions
  55. RunService.RenderStepped:Connect(function()
  56.     AngleH = AngleH - DeltaX/SensitivityX
  57.     DeltaX = 0
  58.     AngleV = math.clamp(AngleV - DeltaY/SensitivityY, MinY, MaxY)
  59.     DeltaY = 0
  60.     local FinalCFrame = CFrame.new(HumanoidRootPart.Position) * CFrame.Angles(0, AngleH, 0) * CFrame.Angles(AngleV, 0, 0) * Offset
  61.     Cam.CoordinateFrame = FinalCFrame
  62.     if(Popper == true) then
  63.         local Direction = (FinalCFrame.p - Character.Head.Position).Unit * ((Offset.p).Magnitude)
  64.         local CheckRay = Ray.new(Character.Head.Position, Direction)
  65.         local Part, Position = game.Workspace:FindPartOnRay(CheckRay, Character, false, true)
  66.         if Part then
  67.             local Distance = Cam:GetLargestCutoffDistance({Character})
  68.             Cam.CoordinateFrame = Cam.CoordinateFrame * CFrame.new(0, 0, -Distance)
  69.         end
  70.     end
  71.     if(W == true) or (A == true) or (S == true) or (D == true) then
  72.         for Num, Val in pairs(Combinations) do
  73.             if(Val[1] == W) and (Val[2] == A) and (Val[3] == S) and (Val[4] == D) then
  74.                 local DirectionVector = Cam.CoordinateFrame.lookVector
  75.                 local Position = HumanoidRootPart.Position
  76.                 local TargetCFrame = CFrame.new(HumanoidRootPart.Position, HumanoidRootPart.Position + Vector3.new(DirectionVector.X, 0, DirectionVector.Z))
  77.                 HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:lerp(TargetCFrame * CFrame.Angles(0, Val[5], 0), 0.25)
  78.             end
  79.         end    
  80.     end
  81. end)
  82.  
  83. UIS.InputChanged:Connect(function(Input, Bool)
  84.     if(Bool == false) then
  85.         if(Input.UserInputType == Enum.UserInputType.MouseMovement) then
  86.             if(DeltaX ~= Input.Delta.X) then
  87.                 DeltaX = Input.Delta.X
  88.             end
  89.             if(DeltaY ~= Input.Delta.Y) then
  90.                 DeltaY = Input.Delta.Y
  91.             end
  92.         end
  93.     end
  94. end)
  95.  
  96. UIS.InputBegan:Connect(function(Input, Bool)
  97.     if(Bool == false) then
  98.         if(Input.KeyCode == Enum.KeyCode.W) then
  99.             W = true
  100.         elseif(Input.KeyCode == Enum.KeyCode.A) then
  101.             A = true
  102.         elseif(Input.KeyCode == Enum.KeyCode.S) then
  103.             S = true
  104.         elseif(Input.KeyCode == Enum.KeyCode.D) then
  105.             D = true
  106.         end
  107.     end
  108. end)
  109.  
  110. UIS.InputEnded:Connect(function(Input, Bool)
  111.     if(Bool == false) then
  112.         if(Input.KeyCode == Enum.KeyCode.W) then
  113.             W = false
  114.         elseif(Input.KeyCode == Enum.KeyCode.A) then
  115.             A = false
  116.         elseif(Input.KeyCode == Enum.KeyCode.S) then
  117.             S = false
  118.         elseif(Input.KeyCode == Enum.KeyCode.D) then
  119.             D = false
  120.         end
  121.     end
  122. end)
Add Comment
Please, Sign In to add comment