Advertisement
Vaeb

Vaeb's Camera Interpolation

Apr 27th, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local LP = game:GetService("Players").LocalPlayer
  2. local Char = LP.Character
  3.  
  4. local Camera = workspace.CurrentCamera
  5. local RenderStepped = game:GetService("RunService").RenderStepped
  6.  
  7. local OverrideNum = 1
  8. local Operating = false
  9.  
  10. Camera.CameraType = "Scriptable"
  11.  
  12. function TweenCamera(EndPos, EndFocus, Override, WaitForCompletion, TweenTime, StayFocused, useSin)
  13.     local function TweenFunc()
  14.         if Override and Operating then
  15.             OverrideNum = OverrideNum+1
  16.         elseif Operating then
  17.             return false
  18.         end
  19.         Operating = true
  20.  
  21.         local OverrideID = OverrideNum
  22.         local Interval = (1/(TweenTime/RenderStepped:wait()))
  23.  
  24.         local StartCF = Camera.CFrame
  25.         local EndCF = CFrame.new(EndPos, EndFocus)
  26.  
  27.         for i = 0, 1, Interval do
  28.             i = useSin and math.sin(math.pi/2*i) or i
  29.  
  30.             local newCamCF = StartCF:lerp(EndCF, i)
  31.             Camera.CFrame = (not StayFocused and newCamCF) or CFrame.new(newCamCF.p, EndFocus)
  32.  
  33.             RenderStepped:wait() --Use RenderStepped for anything camera related!!!
  34.  
  35.             if OverrideNum ~= OverrideID then
  36.                 return false
  37.             end
  38.         end
  39.  
  40.         Operating = false
  41.         Camera.CFrame = EndCF
  42.         return true
  43.     end
  44.  
  45.     if WaitForCompletion then
  46.         TweenFunc()
  47.     else
  48.         coroutine.resume(coroutine.create(TweenFunc))
  49.     end
  50. end
  51.  
  52. ---EXAMPLE USAGE---
  53.  
  54. local StartCamOffset = Char.Head.CFrame:toObjectSpace(Camera.CFrame) --(Gets original camera offset to use later)
  55.  
  56. TweenCamera(Vector3.new(60, 60, 60), Vector3.new(10, 60, 70), true, true, 3, false, true)
  57.  
  58. --Key: Description [Parameter]
  59.  
  60. --Tweens position to Vector3.new(60, 60, 60) pointing towards Vector3.new(10, 60, 70) in a half sine wave effect [1] [2] [7]
  61. --Doesn't always point towards Vector3.new(10, 60, 70) (focus will tween too) [6]
  62. --Yields return until the 3 seconds of interpolation has completed [4] [5]
  63. --The interpolation can be overriden at any time by calling the function midway [3]
  64.  
  65. TweenCamera(Vector3.new(40, 50, 30), Vector3.new(-30, 40, -50), true, true, 2, false, true)
  66.  
  67. TweenCamera((Char.Head.CFrame * StartCamOffset).p, Char.Head.CFrame.p, true, true, 4, false, false) --Tweens back to the original offset
  68.  
  69. Camera.CameraSubject = Char.Humanoid
  70. Camera.CameraType = "Custom"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement