alexop1000

camera for roblox

Oct 1st, 2018
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. -- services
  2.  
  3. local UserInputService  = game:GetService("UserInputService")
  4. local RunService        = game:GetService("RunService")
  5. local Workspace         = game:GetService("Workspace")
  6. local Players           = game:GetService("Players")
  7.  
  8. -- constants
  9.  
  10. local PLAYER        = Players.LocalPlayer
  11. local CAMERA        = Workspace.CurrentCamera
  12. local IGNORE        = Workspace:WaitForChild("Ignore")
  13. local CHARACTERS    = Workspace:WaitForChild("Characters")
  14. local CHARACTER     = CHARACTERS:WaitForChild(PLAYER.Name)
  15. local BIKE          = IGNORE:WaitForChild(PLAYER.Name .. "Bike")
  16. local CHAR          = IGNORE:WaitForChild(PLAYER.Name .. "Character")
  17.  
  18. -- variables
  19.  
  20. local modes = {
  21.     "Orbit";
  22.     "Follow";
  23.     "FPS";
  24.     "Track";
  25.     "Gopro";
  26.     "Gopro2";
  27. }
  28.  
  29. local mode  = 1
  30.  
  31. local x,= 0, 0
  32.  
  33. -- functions
  34.  
  35. local function Lerp(a, b, d) return a + (b - a) * d end
  36.  
  37. -- initiate
  38.  
  39. UserInputService.MouseIconEnabled   = true
  40. --UserInputService.MouseBehavior        = Enum.MouseBehavior.LockCenter
  41.  
  42. CAMERA.FieldOfView      = 80
  43. CAMERA.CameraType       = Enum.CameraType.Custom
  44. CAMERA.CameraSubject    = CHARACTER
  45.  
  46. RunService:BindToRenderStep("Camera", Enum.RenderPriority.Last.Value, function(deltaTime)
  47.     CAMERA.FieldOfView  = Lerp(CAMERA.FieldOfView, 80 + CHARACTER.Velocity.Magnitude / 5, math.min(deltaTime * 5, 1))
  48.    
  49.     if modes[mode] == "FPS" then
  50.         CHAR.Head.LocalTransparencyModifier = 1
  51.     else
  52.         CHAR.Head.LocalTransparencyModifier = 0
  53.     end
  54.    
  55.     if modes[mode] == "Orbit" then
  56.         local center    = CHARACTER.Position + Vector3.new(0, 2, 0)
  57.         local cframe    = CFrame.new(center) * CFrame.Angles(0, x, 0) * CFrame.Angles(y, 0, 0) * CFrame.new(0, 0, 20)
  58.         local rotation  = cframe - cframe.p
  59.        
  60.         local ray           = Ray.new(center, (cframe.p - CHARACTER.Position))
  61.         local _, position   = Workspace:FindPartOnRayWithIgnoreList(ray, {CHARACTERS, IGNORE})
  62.         CAMERA.CFrame       = CFrame.new(position) * rotation
  63.     elseif modes[mode] == "Follow" then
  64.         CAMERA.CFrame   = CAMERA.CFrame:Lerp(BIKE.Frame.CFrame * CFrame.new(0, 5, 10), math.min(deltaTime * 2, 1))
  65.     elseif modes[mode] == "FPS" then
  66.         local offset    = CHAR.Hips.CFrame:toObjectSpace(CHAR.Torso.CFrame)
  67.         CAMERA.CFrame   = CHAR.Head.CFrame * CFrame.Angles(-offset.Z * 0.2, 0, 0)
  68.     elseif modes[mode] == "Track" then
  69.         local distance  = (CAMERA.CFrame.p - CHARACTER.Position).Magnitude
  70.        
  71.         CAMERA.FieldOfView  = math.clamp(100 - distance, 10, 90)
  72.         CAMERA.CFrame   = CAMERA.CFrame:Lerp(CFrame.new(CAMERA.CFrame.p, CHARACTER.Position), math.min(deltaTime * 5, 1))
  73.     elseif modes[mode] == "Gopro" then
  74.         CAMERA.FieldOfView  = 100
  75.         CAMERA.CFrame       = BIKE.Frame.CFrame * CFrame.new(2, 0, 2)
  76.     elseif modes[mode] == "Gopro2" then
  77.         CAMERA.FieldOfView  = 100
  78.         CAMERA.CFrame       = BIKE.Frame.CFrame * CFrame.new(0, 3, -1.2)
  79.     end
  80.    
  81.     CAMERA.Focus    = CFrame.new(CHARACTER.Position)
  82. end)
  83.  
  84. -- events
  85.  
  86. UserInputService.InputChanged:connect(function(inputObject, processed)
  87.     if not processed then
  88.         if modes[mode] == "Orbit" then
  89.             x   = (x - inputObject.Delta.X * (1/260)) % (math.pi * 2)
  90.             y   = math.clamp(y - inputObject.Delta.Y * (1/260), -1.4, 1.4)
  91.         end
  92.     end
  93. end)
  94.  
  95. UserInputService.InputBegan:connect(function(inputObject, processed)
  96.     if not processed then
  97.         if inputObject.KeyCode == Enum.KeyCode.C then
  98.             mode    = mode + 1
  99.             if mode > #modes then
  100.                 mode    = 1
  101.             end
  102.            
  103.             if modes[mode] == "Orbit" then
  104.                 local lookVector    = CAMERA.CFrame.lookVector
  105.                 x   = math.atan2(-lookVector.X, -lookVector.Z)
  106.                 y   = math.clamp(math.asin(lookVector.Y), -1.4, 1.4)
  107.             end
  108.         end
  109.     end
  110. end)
Advertisement
Add Comment
Please, Sign In to add comment