sagat1719

Untitled

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