Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local knit = require(game.ReplicatedStorage.Packages.Knit)
- local cameraController = knit.CreateController(
- {
- Name = "CameraController"
- }
- )
- -- # SERVICES
- local rs = game:GetService("RunService")
- local uis = game:GetService("UserInputService")
- local cas = game:GetService("ContextActionService")
- local collection = game:GetService("CollectionService")
- local tween = game:GetService("TweenService")
- local playerService
- -- # VARIABLES
- local floor = math.floor
- local upVector = Vector3.new(0,1,0)
- local CFnew = CFrame.new
- local Vnew = Vector3.new
- local CFAngles = CFrame.Angles
- local clamp = math.clamp
- -- # PREMADE FUNCTIONS
- local function lerp(beginning, goal, alpha)
- return (1 - alpha) * beginning + alpha * goal
- end
- -- # NEVERMORE STUFF
- local Maid = _G.Nevermore("Maid")
- -- # KNIT STUFF
- function cameraController:KnitInit()
- self.Maid = Maid.new()
- end
- function cameraController:KnitStart()
- playerService = knit.GetService("PlayerService")
- end
- -- # CONTROLLER
- function cameraController:UnLoad()
- rs:UnbindFromRenderStep("CameraControl")
- self.Maid:DoCleaning()
- self.PhysicRender = false
- end
- function cameraController:Load()
- local plr = game.Players.LocalPlayer
- self.Character = plr.Character or plr.CharacterAdded:Wait()
- self.Maid:GiveTask(plr.CharacterRemoving:Connect(function()
- self:UnLoad()
- end))
- local root = self.Character:WaitForChild("HumanoidRootPart")
- local hum = self.Character:WaitForChild("Humanoid")
- local mouse = plr:GetMouse()
- uis.MouseBehavior = Enum.MouseBehavior.Default
- self.PhysicRender = true
- local lastRenderDt = 0
- local Interp = 30
- local MinZoom = 5
- local MaxZoom = 25
- local DeltaX = 0
- local DeltaY = 0
- local AngleH = 0
- local AngleV = 0
- self.Camera = workspace.CurrentCamera
- self.Camera.CameraSubject = self.Character:WaitForChild("Humanoid")
- self.SensitivityY = 120
- self.SensitivityX = 120
- self.CameraDistance = 10
- self.UpOffset = 2.25
- self.HorizontalOffset = 0
- local vecoffset
- local Offset = CFrame.new(self.HorizontalOffset, self.UpOffset, self.CameraDistance)
- self.CameraSubject = self.Character.PrimaryPart
- local MaxY = 5*math.pi/12
- local MinY = -5*math.pi/12
- self.MouseMode = true
- self.ShiftLock = false
- self.Popper = true
- self.Disabled = false
- local raycastParams = RaycastParams.new()
- raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
- raycastParams.FilterDescendantsInstances = {self.Character}
- raycastParams.IgnoreWater = true
- -- # CORE GUI
- --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
- --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
- --game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
- self.Maid:GiveTask(uis.InputBegan:Connect(function(inp,pro)
- if pro then return end
- if inp.KeyCode == Enum.KeyCode.LeftShift then
- if self.ShiftLock == false then
- self.ShiftLock = true
- uis.MouseBehavior = Enum.MouseBehavior.LockCenter
- else
- self.ShiftLock = false
- uis.MouseBehavior = Enum.MouseBehavior.Default
- end
- end
- if inp.UserInputType == Enum.UserInputType.MouseButton2 and uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter and self.ShiftLock == false then
- uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
- self.MouseMode = false
- end
- end))
- self.Maid:GiveTask(uis.InputEnded:Connect(function(inp,pro)
- if pro then return end
- if inp.UserInputType == Enum.UserInputType.MouseButton2 and uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter and self.ShiftLock == false then
- uis.MouseBehavior = Enum.MouseBehavior.Default
- self.MouseMode = true
- end
- end))
- self.Maid:GiveTask(uis.InputChanged:Connect(function(Input, Bool)
- if Bool == false then
- if Input.UserInputType == Enum.UserInputType.MouseWheel then
- if self.CameraDistance - Input.Position.Z < MaxZoom and self.CameraDistance - Input.Position.Z > MinZoom then
- --uis.MouseBehavior = Enum.MouseBehavior.LockCenter
- self.CameraDistance -= Input.Position.Z * 1.25
- Offset = CFrame.new(self.HorizontalOffset, self.UpOffset, self.CameraDistance)
- else
- return
- end
- end
- if Input.UserInputType == Enum.UserInputType.MouseMovement then
- local Delta2 = uis:GetMouseDelta()
- if DeltaX ~= Delta2.X then
- DeltaX = Delta2.X
- end
- if DeltaY ~= Delta2.Y then
- DeltaY = Delta2.Y
- end
- end
- end
- end))
- local function OnCameraUpdate(dt)
- if self.Disabled == false then
- self.Camera.CameraType = Enum.CameraType.Scriptable
- local FinalCFrame
- AngleH = AngleH - DeltaX/self.SensitivityX
- DeltaX = 0
- AngleV = clamp(AngleV - DeltaY/self.SensitivityY, MinY, MaxY)
- DeltaY = 0
- FinalCFrame = CFnew(self.CameraSubject.Position) * CFAngles(0, AngleH, 0) * CFAngles(AngleV, 0, 0) * Offset
- if self.Popper == true then
- local Direction = (FinalCFrame.Position - self.CameraSubject.Position).Unit * ((Offset.Position).Magnitude)
- local raycastResult = workspace:Raycast(self.CameraSubject.Position, Direction, raycastParams)
- if raycastResult ~= nil and raycastResult.Instance ~= nil and raycastResult.Instance.CanCollide == true then
- local Distance = (raycastResult.Position - FinalCFrame.Position).Magnitude
- FinalCFrame = FinalCFrame * CFnew(0, 0, -Distance)
- end
- end
- self.Camera.CFrame = self.Camera.CFrame:Lerp(FinalCFrame, lastRenderDt * Interp)
- end
- end
- self.PhysicRender = true
- coroutine.wrap(function()
- while self.PhysicRender do
- local tempTime
- tempTime, lastRenderDt = rs.Stepped:Wait() -- we need that, so camera doesnt jitter when moving to character
- end
- end)()
- rs:BindToRenderStep("CameraControl", Enum.RenderPriority.Camera.Value + 1, OnCameraUpdate)
- self.Maid:GiveTask(playerService.EditCameraController:Connect(function(args)
- self:ChangeSettings(args)
- end))
- end
- function cameraController:ChangeSettings(args)
- for i,v in pairs(args) do
- self[i] = v
- end
- end
- return cameraController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement