Advertisement
Znimatorr

better camera, cool

May 12th, 2022
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.01 KB | None | 0 0
  1. local knit = require(game.ReplicatedStorage.Packages.Knit)
  2.  
  3. local cameraController = knit.CreateController(
  4.     {
  5.         Name = "CameraController"
  6.     }
  7. )
  8.  
  9. -- # SERVICES
  10. local rs = game:GetService("RunService")
  11. local uis = game:GetService("UserInputService")
  12. local cas = game:GetService("ContextActionService")
  13. local collection = game:GetService("CollectionService")
  14. local tween = game:GetService("TweenService")
  15.  
  16. local playerService
  17.  
  18. -- # VARIABLES
  19. local floor = math.floor
  20. local upVector = Vector3.new(0,1,0)
  21. local CFnew = CFrame.new
  22. local Vnew = Vector3.new
  23. local CFAngles = CFrame.Angles
  24. local clamp = math.clamp
  25.  
  26. -- # PREMADE FUNCTIONS
  27. local function lerp(beginning, goal, alpha)
  28.     return (1 - alpha) * beginning + alpha * goal
  29. end
  30.  
  31. -- # NEVERMORE STUFF
  32. local Maid = _G.Nevermore("Maid")
  33.  
  34. -- # KNIT STUFF
  35. function cameraController:KnitInit()
  36.     self.Maid = Maid.new()
  37. end
  38.  
  39. function cameraController:KnitStart()
  40.     playerService = knit.GetService("PlayerService")
  41. end
  42.  
  43. -- # CONTROLLER
  44. function cameraController:UnLoad()
  45.     rs:UnbindFromRenderStep("CameraControl")
  46.     self.Maid:DoCleaning()
  47.     self.PhysicRender = false
  48. end
  49.  
  50. function cameraController:Load()
  51.     local plr = game.Players.LocalPlayer
  52.    
  53.     self.Character = plr.Character or plr.CharacterAdded:Wait()
  54.    
  55.     self.Maid:GiveTask(plr.CharacterRemoving:Connect(function()
  56.         self:UnLoad()
  57.     end))
  58.    
  59.     local root = self.Character:WaitForChild("HumanoidRootPart")
  60.     local hum = self.Character:WaitForChild("Humanoid")
  61.    
  62.     local mouse = plr:GetMouse()
  63.    
  64.     uis.MouseBehavior = Enum.MouseBehavior.Default
  65.    
  66.     self.PhysicRender = true
  67.     local lastRenderDt = 0
  68.    
  69.     local Interp = 30
  70.    
  71.     local MinZoom = 5
  72.     local MaxZoom = 25
  73.    
  74.     local DeltaX = 0
  75.     local DeltaY = 0
  76.    
  77.     local AngleH = 0
  78.     local AngleV = 0
  79.    
  80.     self.Camera = workspace.CurrentCamera
  81.     self.Camera.CameraSubject = self.Character:WaitForChild("Humanoid")
  82.    
  83.     self.SensitivityY = 120
  84.     self.SensitivityX = 120
  85.    
  86.     self.CameraDistance = 10
  87.     self.UpOffset = 2.25
  88.     self.HorizontalOffset = 0
  89.    
  90.     local vecoffset
  91.    
  92.     local Offset = CFrame.new(self.HorizontalOffset, self.UpOffset, self.CameraDistance)
  93.     self.CameraSubject = self.Character.PrimaryPart
  94.    
  95.     local MaxY = 5*math.pi/12
  96.     local MinY = -5*math.pi/12
  97.    
  98.     self.MouseMode = true
  99.     self.ShiftLock = false
  100.     self.Popper = true
  101.     self.Disabled = false
  102.    
  103.     local raycastParams = RaycastParams.new()
  104.     raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
  105.     raycastParams.FilterDescendantsInstances = {self.Character}
  106.     raycastParams.IgnoreWater = true
  107.  
  108.     -- # CORE GUI
  109.     --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
  110.     --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  111.     --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
  112.    
  113.     self.Maid:GiveTask(uis.InputBegan:Connect(function(inp,pro)
  114.         if pro then return end
  115.         if inp.KeyCode == Enum.KeyCode.LeftShift then
  116.             if self.ShiftLock == false then
  117.                 self.ShiftLock = true
  118.                 uis.MouseBehavior = Enum.MouseBehavior.LockCenter
  119.             else
  120.                 self.ShiftLock = false
  121.                 uis.MouseBehavior = Enum.MouseBehavior.Default
  122.             end
  123.         end
  124.         if inp.UserInputType == Enum.UserInputType.MouseButton2 and uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter and self.ShiftLock == false then
  125.             uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
  126.             self.MouseMode = false
  127.         end
  128.     end))
  129.  
  130.     self.Maid:GiveTask(uis.InputEnded:Connect(function(inp,pro)
  131.         if pro then return end
  132.         if inp.UserInputType == Enum.UserInputType.MouseButton2 and uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter and self.ShiftLock == false then
  133.             uis.MouseBehavior = Enum.MouseBehavior.Default
  134.             self.MouseMode = true
  135.         end
  136.     end))
  137.    
  138.     self.Maid:GiveTask(uis.InputChanged:Connect(function(Input, Bool)
  139.         if Bool == false then
  140.             if Input.UserInputType == Enum.UserInputType.MouseWheel then
  141.                 if self.CameraDistance - Input.Position.Z < MaxZoom and self.CameraDistance - Input.Position.Z > MinZoom then
  142.                     --uis.MouseBehavior = Enum.MouseBehavior.LockCenter
  143.                     self.CameraDistance -= Input.Position.Z * 1.25
  144.                     Offset = CFrame.new(self.HorizontalOffset, self.UpOffset, self.CameraDistance)
  145.                 else
  146.                     return
  147.                 end
  148.             end
  149.             if Input.UserInputType == Enum.UserInputType.MouseMovement then
  150.                 local Delta2 = uis:GetMouseDelta()
  151.                 if DeltaX ~= Delta2.X then
  152.                     DeltaX = Delta2.X
  153.                 end
  154.                 if DeltaY ~= Delta2.Y then
  155.                     DeltaY = Delta2.Y
  156.                 end
  157.             end
  158.         end
  159.     end))
  160.    
  161.     local function OnCameraUpdate(dt)
  162.         if self.Disabled == false then
  163.        
  164.             self.Camera.CameraType = Enum.CameraType.Scriptable
  165.  
  166.             local FinalCFrame
  167.  
  168.             AngleH = AngleH - DeltaX/self.SensitivityX
  169.             DeltaX = 0
  170.             AngleV = clamp(AngleV - DeltaY/self.SensitivityY, MinY, MaxY)
  171.             DeltaY = 0
  172.                
  173.             FinalCFrame = CFnew(self.CameraSubject.Position) * CFAngles(0, AngleH, 0) * CFAngles(AngleV, 0, 0) * Offset    
  174.                
  175.             if self.Popper == true then
  176.                 local Direction = (FinalCFrame.Position - self.CameraSubject.Position).Unit * ((Offset.Position).Magnitude)
  177.  
  178.                 local raycastResult = workspace:Raycast(self.CameraSubject.Position, Direction, raycastParams)
  179.  
  180.                 if raycastResult ~= nil and raycastResult.Instance ~= nil and raycastResult.Instance.CanCollide == true then
  181.                     local Distance = (raycastResult.Position - FinalCFrame.Position).Magnitude
  182.                     FinalCFrame = FinalCFrame * CFnew(0, 0, -Distance)
  183.                 end
  184.             end
  185.                
  186.             self.Camera.CFrame = self.Camera.CFrame:Lerp(FinalCFrame, lastRenderDt * Interp)
  187.         end
  188.     end
  189.    
  190.     self.PhysicRender = true
  191.     coroutine.wrap(function()
  192.         while self.PhysicRender do
  193.             local tempTime
  194.             tempTime, lastRenderDt = rs.Stepped:Wait() -- we need that, so camera doesnt jitter when moving to character
  195.         end
  196.     end)()
  197.    
  198.     rs:BindToRenderStep("CameraControl", Enum.RenderPriority.Camera.Value + 1, OnCameraUpdate)
  199.    
  200.     self.Maid:GiveTask(playerService.EditCameraController:Connect(function(args)
  201.         self:ChangeSettings(args)
  202.     end))
  203. end
  204.  
  205. function cameraController:ChangeSettings(args)
  206.     for i,v in pairs(args) do
  207.         self[i] = v
  208.     end
  209. end
  210.  
  211. return cameraController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement