Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Myself:Player? = game:GetService("Players").LocalPlayer
- local MyMouse:Mouse = Myself:GetMouse()
- local MyCharacter:Model = script.Parent
- local MyCamera:Camera = workspace.CurrentCamera
- local UserInput:UserInputService = game:GetService("UserInputService")
- local RunService:RunService = game:GetService("RunService")
- local Storage = game:GetService('ReplicatedStorage')
- local Keycodes = Enum.KeyCode
- local InputTypes = Enum.UserInputType
- local ControllerBindings = {ButtonR2 = "Grab", ButtonL1 = "ZoomIn", ButtonR1 = "ZoomOut"}
- local KeyboardBindings = { MouseButton1 = "Grab", Q = "ZoomIn", E = "ZoomOut"}
- local BindingTypes = {GamePad1 = ControllerBindings,MouseButton1 = KeyboardBindings,MouseButton2 = KeyboardBindings,Keyboard = KeyboardBindings}
- local DirectionToCF = {Up = CFrame.Angles(0,math.rad(100),0),Down=CFrame.Angles(0,math.rad(-100),0)}
- local RayParams:RaycastParams = RaycastParams.new()
- RayParams.RespectCanCollide = true
- RayParams.FilterDescendantsInstances = {MyCharacter}
- local GrabOrientation:AlignOrientation = script.AlignOrientation
- local GrabPosition:AlignPosition = script.AlignPosition
- local GrabAttachment:Attachment
- local GrabDetails = {}
- local Zooming = 0
- local Turning:CFrame = CFrame.Angles(0,0,0)
- local ZoomSpeed = 5
- local MaxDistance = 8
- local DistanceBuffer = 4
- ---------------------
- local Checker = require(Storage.Modules.GrabbableChecker)
- local PlateHandler = require(Storage.Modules.PlateHandler)
- function Drop()
- GrabDetails["Part"],GrabDetails["Distance"] = nil
- GrabPosition.Attachment0,GrabOrientation.Attachment0 = nil
- GrabAttachment:Destroy()
- Storage.Events.Release:FireServer()
- end
- function Rotate(Up:number)
- if GrabDetails["Orientation"] then
- if UserInput:IsKeyDown(Enum.KeyCode.LeftControl) then
- GrabDetails["Orientation"] *= CFrame.Angles(math.rad(10 * Up),0,0)
- else
- GrabDetails["Orientation"] *= CFrame.Angles(0,math.rad(10 * Up),0)
- end
- end
- end
- MyMouse.WheelForward:Connect(function() Rotate(1) end)
- MyMouse.WheelBackward:Connect(function() Rotate(-1) end)
- UserInput.InputBegan:Connect(function(input,InGUI)
- if not InGUI then
- local KeyCode = input.KeyCode.Name
- local InputType = input.UserInputType.Name
- local Bindings = BindingTypes[InputType]
- if Bindings then Action = Bindings[InputType] or Bindings[KeyCode] else return; end
- if Action == "Grab" then
- local CameraCF:CFrame = MyCamera.CFrame
- local GrabRay:RaycastResult = workspace:Raycast(CameraCF.Position,CameraCF.LookVector.Unit * MaxDistance,RayParams)
- if GrabRay then
- local Target:Part = GrabRay.Instance
- Checker.Check(Target)
- if Checker.Grabbable then
- PlateHandler.DetachFromPlate(Target)
- GrabDetails["Part"], GrabDetails["Distance"] = Target, GrabRay.Distance
- local GrabOffset = Target:GetAttribute("GrabOffset")
- GrabDetails["Part"],GrabDetails["Distance"] = Target,GrabRay.Distance
- local Offset:CFrame = Target:GetAttribute("GrabOffset")
- if not Offset then
- Offset = CFrame.new(0,0,0)
- GrabDetails["Orientation"] = CameraCF:ToObjectSpace(Target.CFrame)
- else
- GrabDetails["Orientation"] = Offset
- end
- GrabAttachment = Instance.new("Attachment")
- GrabAttachment.Parent = Target
- GrabAttachment.WorldPosition = GrabRay.Position
- GrabPosition.Attachment0,GrabOrientation.Attachment0 = GrabAttachment,GrabAttachment
- GrabAttachment.Destroying:Connect(Drop)
- Storage.Events.Grab:FireServer(Target)
- end
- end
- elseif Action == "ZoomIn" then Zooming = -1
- elseif Action == "ZoomOut" then Zooming = 1
- end
- end
- end)
- UserInput.InputEnded:Connect(function(input)
- local KeyCode = input.KeyCode.Name
- local InputType = input.UserInputType.Name
- local Bindings = BindingTypes[InputType]
- if Bindings then Action = Bindings[KeyCode] or Bindings[InputType]
- else return end
- if Action == "Grab" and GrabDetails["Part"] then Drop()
- elseif Action == "ZoomIn" or Action == "ZoomOut" then Zooming = 0
- elseif DirectionToCF[Action] then Turning = CFrame.Angles(0,0,0) end
- end)
- RunService.Heartbeat:Connect(function(delta)
- local CameraCF:CFrame = MyCamera.CFrame
- if GrabDetails["Distance"] then
- GrabDetails["Distance"] = math.clamp(GrabDetails["Distance"] + Zooming * ZoomSpeed * delta,1,MaxDistance)
- GrabPosition.Position = CameraCF.Position + CameraCF.LookVector.Unit * GrabDetails["Distance"]
- GrabOrientation.CFrame = CameraCF:ToWorldSpace(GrabDetails["Orientation"])
- if (GrabDetails["Part"].Position - CameraCF.Position).Magnitude > MaxDistance + DistanceBuffer then
- Drop()
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment