Advertisement
GroupThingy

eeeee

May 10th, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.27 KB | None | 0 0
  1. --Controls:
  2. --Shift + P = Toggle freecam
  3. --WASD - Move around
  4. --E = Move up
  5. --Q = Move down
  6. --Shift = Slow down camera
  7. --Right Click + Mouse = Look Around
  8. --Mouse Wheel = Camera Zoom
  9.  
  10.  
  11. function sandbox(var,func)
  12. local env = getfenv(func)
  13. local newenv = setmetatable({},{
  14. __index = function(self,k)
  15. if k=="script" then
  16. return var
  17. else
  18. return env[k]
  19. end
  20. end,
  21. })
  22. setfenv(func,newenv)
  23. return func
  24. end
  25. cors = {}
  26. mas = Instance.new("Model",game:GetService("Lighting"))
  27. LocalScript0 = Instance.new("LocalScript")
  28. LocalScript0.Name = "FreeCamera"
  29. LocalScript0.Parent = mas
  30. table.insert(cors,sandbox(LocalScript0,function()
  31. -----------------------------------------------------------------------
  32. -- Freecam
  33. -- Cinematic free camera for spectating and video production.
  34. ------------------------------------------------------------------------
  35.  
  36. local pi = math.pi
  37. local abs = math.abs
  38. local clamp = math.clamp
  39. local exp = math.exp
  40. local rad = math.rad
  41. local sign = math.sign
  42. local sqrt = math.sqrt
  43. local tan = math.tan
  44.  
  45. local ContextActionService = game:GetService("ContextActionService")
  46. local Players = game:GetService("Players")
  47. local RunService = game:GetService("RunService")
  48. local StarterGui = game:GetService("StarterGui")
  49. local UserInputService = game:GetService("UserInputService")
  50.  
  51. local LocalPlayer = Players.LocalPlayer
  52. if not LocalPlayer then
  53. Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
  54. LocalPlayer = Players.LocalPlayer
  55. end
  56.  
  57. local Camera = workspace.CurrentCamera
  58. workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  59. local newCamera = workspace.CurrentCamera
  60. if newCamera then
  61. Camera = newCamera
  62. end
  63. end)
  64.  
  65. ------------------------------------------------------------------------
  66.  
  67. local TOGGLE_INPUT_PRIORITY = Enum.ContextActionPriority.Low.Value
  68. local INPUT_PRIORITY = Enum.ContextActionPriority.High.Value
  69. local FREECAM_MACRO_KB = {Enum.KeyCode.LeftShift, Enum.KeyCode.P}
  70.  
  71. local NAV_GAIN = Vector3.new(1, 1, 1)*64
  72. local PAN_GAIN = Vector2.new(0.75, 1)*8
  73. local FOV_GAIN = 300
  74.  
  75. local PITCH_LIMIT = rad(90)
  76.  
  77. local VEL_STIFFNESS = 1.5
  78. local PAN_STIFFNESS = 1.0
  79. local FOV_STIFFNESS = 4.0
  80.  
  81. ------------------------------------------------------------------------
  82.  
  83. local Spring = {} do
  84. Spring.__index = Spring
  85.  
  86. function Spring.new(freq, pos)
  87. local self = setmetatable({}, Spring)
  88. self.f = freq
  89. self.p = pos
  90. self.v = pos*0
  91. return self
  92. end
  93.  
  94. function Spring:Update(dt, goal)
  95. local f = self.f*2*pi
  96. local p0 = self.p
  97. local v0 = self.v
  98.  
  99. local offset = goal - p0
  100. local decay = exp(-f*dt)
  101.  
  102. local p1 = goal + (v0*dt - offset*(f*dt + 1))*decay
  103. local v1 = (f*dt*(offset*f - v0) + v0)*decay
  104.  
  105. self.p = p1
  106. self.v = v1
  107.  
  108. return p1
  109. end
  110.  
  111. function Spring:Reset(pos)
  112. self.p = pos
  113. self.v = pos*0
  114. end
  115. end
  116.  
  117. ------------------------------------------------------------------------
  118.  
  119. local cameraPos = Vector3.new()
  120. local cameraRot = Vector2.new()
  121. local cameraFov = 0
  122.  
  123. local velSpring = Spring.new(VEL_STIFFNESS, Vector3.new())
  124. local panSpring = Spring.new(PAN_STIFFNESS, Vector2.new())
  125. local fovSpring = Spring.new(FOV_STIFFNESS, 0)
  126.  
  127. ------------------------------------------------------------------------
  128.  
  129. local Input = {} do
  130. local thumbstickCurve do
  131. local K_CURVATURE = 2.0
  132. local K_DEADZONE = 0.15
  133.  
  134. local function fCurve(x)
  135. return (exp(K_CURVATURE*x) - 1)/(exp(K_CURVATURE) - 1)
  136. end
  137.  
  138. local function fDeadzone(x)
  139. return fCurve((x - K_DEADZONE)/(1 - K_DEADZONE))
  140. end
  141.  
  142. function thumbstickCurve(x)
  143. return sign(x)*clamp(fDeadzone(abs(x)), 0, 1)
  144. end
  145. end
  146.  
  147. local gamepad = {
  148. ButtonX = 0,
  149. ButtonY = 0,
  150. DPadDown = 0,
  151. DPadUp = 0,
  152. ButtonL2 = 0,
  153. ButtonR2 = 0,
  154. Thumbstick1 = Vector2.new(),
  155. Thumbstick2 = Vector2.new(),
  156. }
  157.  
  158. local keyboard = {
  159. W = 0,
  160. A = 0,
  161. S = 0,
  162. D = 0,
  163. E = 0,
  164. Q = 0,
  165. U = 0,
  166. H = 0,
  167. J = 0,
  168. K = 0,
  169. I = 0,
  170. Y = 0,
  171. Up = 0,
  172. Down = 0,
  173. LeftShift = 0,
  174. RightShift = 0,
  175. }
  176.  
  177. local mouse = {
  178. Delta = Vector2.new(),
  179. MouseWheel = 0,
  180. }
  181.  
  182. local NAV_GAMEPAD_SPEED = Vector3.new(1, 1, 1)
  183. local NAV_KEYBOARD_SPEED = Vector3.new(1, 1, 1)
  184. local PAN_MOUSE_SPEED = Vector2.new(1, 1)*(pi/64)
  185. local PAN_GAMEPAD_SPEED = Vector2.new(1, 1)*(pi/8)
  186. local FOV_WHEEL_SPEED = 1.0
  187. local FOV_GAMEPAD_SPEED = 0.25
  188. local NAV_ADJ_SPEED = 0.75
  189. local NAV_SHIFT_MUL = 0.25
  190.  
  191. local navSpeed = 1
  192.  
  193. function Input.Vel(dt)
  194. navSpeed = clamp(navSpeed + dt*(keyboard.Up - keyboard.Down)*NAV_ADJ_SPEED, 0.01, 4)
  195.  
  196. local kGamepad = Vector3.new(
  197. thumbstickCurve(gamepad.Thumbstick1.x),
  198. thumbstickCurve(gamepad.ButtonR2) - thumbstickCurve(gamepad.ButtonL2),
  199. thumbstickCurve(-gamepad.Thumbstick1.y)
  200. )*NAV_GAMEPAD_SPEED
  201.  
  202. local kKeyboard = Vector3.new(
  203. keyboard.D - keyboard.A + keyboard.K - keyboard.H,
  204. keyboard.E - keyboard.Q + keyboard.I - keyboard.Y,
  205. keyboard.S - keyboard.W + keyboard.J - keyboard.U
  206. )*NAV_KEYBOARD_SPEED
  207.  
  208. local shift = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) or UserInputService:IsKeyDown(Enum.KeyCode.RightShift)
  209.  
  210. return (kGamepad + kKeyboard)*(navSpeed*(shift and NAV_SHIFT_MUL or 1))
  211. end
  212.  
  213. function Input.Pan(dt)
  214. local kGamepad = Vector2.new(
  215. thumbstickCurve(gamepad.Thumbstick2.y),
  216. thumbstickCurve(-gamepad.Thumbstick2.x)
  217. )*PAN_GAMEPAD_SPEED
  218. local kMouse = mouse.Delta*PAN_MOUSE_SPEED
  219. mouse.Delta = Vector2.new()
  220. return kGamepad + kMouse
  221. end
  222.  
  223. function Input.Fov(dt)
  224. local kGamepad = (gamepad.ButtonX - gamepad.ButtonY)*FOV_GAMEPAD_SPEED
  225. local kMouse = mouse.MouseWheel*FOV_WHEEL_SPEED
  226. mouse.MouseWheel = 0
  227. return kGamepad + kMouse
  228. end
  229.  
  230. do
  231. local function Keypress(action, state, input)
  232. keyboard[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  233. return Enum.ContextActionResult.Sink
  234. end
  235.  
  236. local function GpButton(action, state, input)
  237. gamepad[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  238. return Enum.ContextActionResult.Sink
  239. end
  240.  
  241. local function MousePan(action, state, input)
  242. local delta = input.Delta
  243. mouse.Delta = Vector2.new(-delta.y, -delta.x)
  244. return Enum.ContextActionResult.Sink
  245. end
  246.  
  247. local function Thumb(action, state, input)
  248. gamepad[input.KeyCode.Name] = input.Position
  249. return Enum.ContextActionResult.Sink
  250. end
  251.  
  252. local function Trigger(action, state, input)
  253. gamepad[input.KeyCode.Name] = input.Position.z
  254. return Enum.ContextActionResult.Sink
  255. end
  256.  
  257. local function MouseWheel(action, state, input)
  258. mouse[input.UserInputType.Name] = -input.Position.z
  259. return Enum.ContextActionResult.Sink
  260. end
  261.  
  262. local function Zero(t)
  263. for k, v in pairs(t) do
  264. t[k] = v*0
  265. end
  266. end
  267.  
  268. function Input.StartCapture()
  269. ContextActionService:BindActionAtPriority("FreecamKeyboard", Keypress, false, INPUT_PRIORITY,
  270. Enum.KeyCode.W, Enum.KeyCode.U,
  271. Enum.KeyCode.A, Enum.KeyCode.H,
  272. Enum.KeyCode.S, Enum.KeyCode.J,
  273. Enum.KeyCode.D, Enum.KeyCode.K,
  274. Enum.KeyCode.E, Enum.KeyCode.I,
  275. Enum.KeyCode.Q, Enum.KeyCode.Y,
  276. Enum.KeyCode.Up, Enum.KeyCode.Down
  277. )
  278. ContextActionService:BindActionAtPriority("FreecamMousePan", MousePan, false, INPUT_PRIORITY, Enum.UserInputType.MouseMovement)
  279. ContextActionService:BindActionAtPriority("FreecamMouseWheel", MouseWheel, false, INPUT_PRIORITY, Enum.UserInputType.MouseWheel)
  280. ContextActionService:BindActionAtPriority("FreecamGamepadButton", GpButton, false, INPUT_PRIORITY, Enum.KeyCode.ButtonX, Enum.KeyCode.ButtonY)
  281. ContextActionService:BindActionAtPriority("FreecamGamepadTrigger", Trigger, false, INPUT_PRIORITY, Enum.KeyCode.ButtonR2, Enum.KeyCode.ButtonL2)
  282. ContextActionService:BindActionAtPriority("FreecamGamepadThumbstick", Thumb, false, INPUT_PRIORITY, Enum.KeyCode.Thumbstick1, Enum.KeyCode.Thumbstick2)
  283. end
  284.  
  285. function Input.StopCapture()
  286. navSpeed = 1
  287. Zero(gamepad)
  288. Zero(keyboard)
  289. Zero(mouse)
  290. ContextActionService:UnbindAction("FreecamKeyboard")
  291. ContextActionService:UnbindAction("FreecamMousePan")
  292. ContextActionService:UnbindAction("FreecamMouseWheel")
  293. ContextActionService:UnbindAction("FreecamGamepadButton")
  294. ContextActionService:UnbindAction("FreecamGamepadTrigger")
  295. ContextActionService:UnbindAction("FreecamGamepadThumbstick")
  296. end
  297. end
  298. end
  299.  
  300. local function GetFocusDistance(cameraFrame)
  301. local znear = 0.1
  302. local viewport = Camera.ViewportSize
  303. local projy = 2*tan(cameraFov/2)
  304. local projx = viewport.x/viewport.y*projy
  305. local fx = cameraFrame.rightVector
  306. local fy = cameraFrame.upVector
  307. local fz = cameraFrame.lookVector
  308.  
  309. local minVect = Vector3.new()
  310. local minDist = 512
  311.  
  312. for x = 0, 1, 0.5 do
  313. for y = 0, 1, 0.5 do
  314. local cx = (x - 0.5)*projx
  315. local cy = (y - 0.5)*projy
  316. local offset = fx*cx - fy*cy + fz
  317. local origin = cameraFrame.p + offset*znear
  318. local part, hit = workspace:FindPartOnRay(Ray.new(origin, offset.unit*minDist))
  319. local dist = (hit - origin).magnitude
  320. if minDist > dist then
  321. minDist = dist
  322. minVect = offset.unit
  323. end
  324. end
  325. end
  326.  
  327. return fz:Dot(minVect)*minDist
  328. end
  329.  
  330. ------------------------------------------------------------------------
  331.  
  332. local function StepFreecam(dt)
  333. local vel = velSpring:Update(dt, Input.Vel(dt))
  334. local pan = panSpring:Update(dt, Input.Pan(dt))
  335. local fov = fovSpring:Update(dt, Input.Fov(dt))
  336.  
  337. local zoomFactor = sqrt(tan(rad(70/2))/tan(rad(cameraFov/2)))
  338.  
  339. cameraFov = clamp(cameraFov + fov*FOV_GAIN*(dt/zoomFactor), 1, 120)
  340. cameraRot = cameraRot + pan*PAN_GAIN*(dt/zoomFactor)
  341. cameraRot = Vector2.new(clamp(cameraRot.x, -PITCH_LIMIT, PITCH_LIMIT), cameraRot.y%(2*pi))
  342.  
  343. local cameraCFrame = CFrame.new(cameraPos)*CFrame.fromOrientation(cameraRot.x, cameraRot.y, 0)*CFrame.new(vel*NAV_GAIN*dt)
  344. cameraPos = cameraCFrame.p
  345.  
  346. Camera.CFrame = cameraCFrame
  347. Camera.Focus = cameraCFrame*CFrame.new(0, 0, -GetFocusDistance(cameraCFrame))
  348. Camera.FieldOfView = cameraFov
  349. end
  350.  
  351. ------------------------------------------------------------------------
  352.  
  353. local PlayerState = {} do
  354. local mouseIconEnabled
  355. local cameraSubject
  356. local cameraType
  357. local cameraFocus
  358. local cameraCFrame
  359. local cameraFieldOfView
  360. local screenGuis = {}
  361. local coreGuis = {
  362. Backpack = true,
  363. Chat = true,
  364. Health = true,
  365. PlayerList = true,
  366. }
  367. local setCores = {
  368. BadgesNotificationsActive = true,
  369. PointsNotificationsActive = true,
  370. }
  371.  
  372. -- Save state and set up for freecam
  373. function PlayerState.Push()
  374. for name in pairs(coreGuis) do
  375. coreGuis[name] = StarterGui:GetCoreGuiEnabled(Enum.CoreGuiType[name])
  376. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], false)
  377. end
  378. for name in pairs(setCores) do
  379. setCores[name] = StarterGui:GetCore(name)
  380. StarterGui:SetCore(name, false)
  381. end
  382. local playergui = LocalPlayer:FindFirstChildOfClass("PlayerGui")
  383. if playergui then
  384. for _, gui in pairs(playergui:GetChildren()) do
  385. if gui:IsA("ScreenGui") and gui.Enabled then
  386. screenGuis[#screenGuis + 1] = gui
  387. gui.Enabled = false
  388. end
  389. end
  390. end
  391.  
  392. cameraFieldOfView = Camera.FieldOfView
  393. Camera.FieldOfView = 70
  394.  
  395. cameraType = Camera.CameraType
  396. Camera.CameraType = Enum.CameraType.Custom
  397.  
  398. cameraSubject = Camera.CameraSubject
  399. Camera.CameraSubject = nil
  400.  
  401. cameraCFrame = Camera.CFrame
  402. cameraFocus = Camera.Focus
  403.  
  404. mouseIconEnabled = UserInputService.MouseIconEnabled
  405. UserInputService.MouseIconEnabled = false
  406.  
  407. mouseBehavior = UserInputService.MouseBehavior
  408. UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  409. end
  410.  
  411. -- Restore state
  412. function PlayerState.Pop()
  413. for name, isEnabled in pairs(coreGuis) do
  414. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], isEnabled)
  415. end
  416. for name, isEnabled in pairs(setCores) do
  417. StarterGui:SetCore(name, isEnabled)
  418. end
  419. for _, gui in pairs(screenGuis) do
  420. if gui.Parent then
  421. gui.Enabled = true
  422. end
  423. end
  424.  
  425. Camera.FieldOfView = cameraFieldOfView
  426. cameraFieldOfView = nil
  427.  
  428. Camera.CameraType = cameraType
  429. cameraType = nil
  430.  
  431. Camera.CameraSubject = cameraSubject
  432. cameraSubject = nil
  433.  
  434. Camera.CFrame = cameraCFrame
  435. cameraCFrame = nil
  436.  
  437. Camera.Focus = cameraFocus
  438. cameraFocus = nil
  439.  
  440. UserInputService.MouseIconEnabled = mouseIconEnabled
  441. mouseIconEnabled = nil
  442.  
  443. UserInputService.MouseBehavior = mouseBehavior
  444. mouseBehavior = nil
  445. end
  446. end
  447.  
  448. local function StartFreecam()
  449. local cameraCFrame = Camera.CFrame
  450. cameraRot = Vector2.new(cameraCFrame:toEulerAnglesYXZ())
  451. cameraPos = cameraCFrame.p
  452. cameraFov = Camera.FieldOfView
  453.  
  454. velSpring:Reset(Vector3.new())
  455. panSpring:Reset(Vector2.new())
  456. fovSpring:Reset(0)
  457.  
  458. PlayerState.Push()
  459. RunService:BindToRenderStep("Freecam", Enum.RenderPriority.Camera.Value, StepFreecam)
  460. Input.StartCapture()
  461. end
  462.  
  463. local function StopFreecam()
  464. Input.StopCapture()
  465. RunService:UnbindFromRenderStep("Freecam")
  466. PlayerState.Pop()
  467. end
  468.  
  469. ------------------------------------------------------------------------
  470.  
  471. do
  472. local enabled = false
  473.  
  474. local function ToggleFreecam()
  475. if enabled then
  476. StopFreecam()
  477. else
  478. StartFreecam()
  479. end
  480. enabled = not enabled
  481. end
  482.  
  483. local function CheckMacro(macro)
  484. for i = 1, #macro - 1 do
  485. if not UserInputService:IsKeyDown(macro[i]) then
  486. return
  487. end
  488. end
  489. ToggleFreecam()
  490. end
  491.  
  492. local function HandleActivationInput(action, state, input)
  493. if state == Enum.UserInputState.Begin then
  494. if input.KeyCode == FREECAM_MACRO_KB[#FREECAM_MACRO_KB] then
  495. CheckMacro(FREECAM_MACRO_KB)
  496. end
  497. end
  498. return Enum.ContextActionResult.Pass
  499. end
  500.  
  501. ContextActionService:BindActionAtPriority("FreecamToggle", HandleActivationInput, false, TOGGLE_INPUT_PRIORITY, FREECAM_MACRO_KB[#FREECAM_MACRO_KB])
  502. end
  503. end))
  504. for i,v in pairs(mas:GetChildren()) do
  505. v.Parent = game:GetService("Players").LocalPlayer.PlayerGui
  506. pcall(function() v:MakeJoints() end)
  507. end
  508. mas:Destroy()
  509. for i,v in pairs(cors) do
  510. spawn(function()
  511. pcall(v)
  512. end)
  513. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement