Advertisement
sneakydodge123

jailbreak

Apr 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.55 KB | None | 0 0
  1. local PlayersService = game:GetService("Players")
  2. local UserInputService = game:GetService("UserInputService")
  3. local StarterGui = game:GetService("StarterGui")
  4. local GuiService = game:GetService("GuiService")
  5. local ContextActionService = game:GetService("ContextActionService")
  6. local VRServiceExists, VRService = pcall(function()
  7. return game:GetService("VRService")
  8. end)
  9. if not VRServiceExists or not VRService then
  10. VRService = UserInputService
  11. end
  12. local CameraScript = script.Parent
  13. local ShiftLockController = require(CameraScript:WaitForChild("ShiftLockController"))
  14. local Settings = UserSettings()
  15. local GameSettings = Settings.GameSettings
  16. local spawnLocationFixFlagExists, spawnLocationFixFlagEnabled = pcall(function()
  17. return Settings:IsUserFeatureEnabled("UserSpawnLocationFixEnabled")
  18. end)
  19. local FFlagSpawnLocationFixEnabled = spawnLocationFixFlagExists and spawnLocationFixFlagEnabled
  20. local clamp = function(low, high, num)
  21. if low <= high then
  22. return math.min(high, math.max(low, num))
  23. end
  24. return num
  25. end
  26. local findAngleBetweenXZVectors = function(vec2, vec1)
  27. return math.atan2(vec1.X * vec2.Z - vec1.Z * vec2.X, vec1.X * vec2.X + vec1.Z * vec2.Z)
  28. end
  29. local IsFinite = function(num)
  30. return num == num and num ~= 1 / 0 and num ~= -1 / 0
  31. end
  32. local THUMBSTICK_DEADZONE = 0.2
  33. local humanoidCache = {}
  34. local function findPlayerHumanoid(player)
  35. local character = player and player.Character
  36. if character then
  37. local resultHumanoid = humanoidCache[player]
  38. if resultHumanoid and resultHumanoid.Parent == character then
  39. return resultHumanoid
  40. else
  41. humanoidCache[player] = nil
  42. for _, child in pairs(character:GetChildren()) do
  43. if child:IsA("Humanoid") then
  44. humanoidCache[player] = child
  45. return child
  46. end
  47. end
  48. end
  49. end
  50. end
  51. local MIN_Y = math.rad(-80)
  52. local MAX_Y = math.rad(80)
  53. local VR_ANGLE = math.rad(15)
  54. local VR_LOW_INTENSITY_ROTATION = Vector2.new(math.rad(15), 0)
  55. local VR_HIGH_INTENSITY_ROTATION = Vector2.new(math.rad(45), 0)
  56. local VR_LOW_INTENSITY_REPEAT = 0.1
  57. local VR_HIGH_INTENSITY_REPEAT = 0.4
  58. local ZERO_VECTOR2 = Vector2.new(0, 0)
  59. local TOUCH_SENSITIVTY = Vector2.new(math.pi * 2.25, math.pi * 2)
  60. local MOUSE_SENSITIVITY = Vector2.new(math.pi * 4, math.pi * 1.9)
  61. local SEAT_OFFSET = Vector3.new(0, 5, 0)
  62. local VR_SEAT_OFFSET = Vector3.new(0, 4, 0)
  63. local HEAD_OFFSET = Vector3.new(0, 1.5, 0)
  64. local R15_HEAD_OFFSET = Vector3.new(0, 2, 0)
  65. local SetCameraOnSpawn = true
  66. local hasGameLoaded = false
  67. local GetRenderCFrame = function(part)
  68. return part:GetRenderCFrame()
  69. end
  70. local function CreateCamera()
  71. local this = {}
  72. local R15HeadHeight = R15_HEAD_OFFSET
  73. function this:GetActivateValue()
  74. return 0.7
  75. end
  76. function this:GetRotateAmountValue(vrRotationIntensity)
  77. vrRotationIntensity = vrRotationIntensity or StarterGui:GetCore("VRRotationIntensity")
  78. if vrRotationIntensity then
  79. if vrRotationIntensity == "Low" then
  80. return VR_LOW_INTENSITY_ROTATION
  81. elseif vrRotationIntensity == "High" then
  82. return VR_HIGH_INTENSITY_ROTATION
  83. end
  84. end
  85. return ZERO_VECTOR2
  86. end
  87. function this:GetRepeatDelayValue(vrRotationIntensity)
  88. vrRotationIntensity = vrRotationIntensity or StarterGui:GetCore("VRRotationIntensity")
  89. if vrRotationIntensity then
  90. if vrRotationIntensity == "Low" then
  91. return VR_LOW_INTENSITY_REPEAT
  92. elseif vrRotationIntensity == "High" then
  93. return VR_HIGH_INTENSITY_REPEAT
  94. end
  95. end
  96. return 0
  97. end
  98. this.ShiftLock = false
  99. this.Enabled = false
  100. local isFirstPerson = false
  101. local isRightMouseDown = false
  102. local isMiddleMouseDown = false
  103. this.RotateInput = Vector2.new()
  104. this.lastSubject = nil
  105. this.lastSubjectPosition = Vector3.new(0, 5, 0)
  106. local lastVRRotation = 0
  107. local vrRotateKeyCooldown = {}
  108. function this:GetShiftLock()
  109. return ShiftLockController:IsShiftLocked()
  110. end
  111. function this:GetHumanoid()
  112. local player = PlayersService.LocalPlayer
  113. return findPlayerHumanoid(player)
  114. end
  115. function this:GetHumanoidRootPart()
  116. local humanoid = this:GetHumanoid()
  117. return humanoid and humanoid.Torso
  118. end
  119. function this:GetRenderCFrame(part)
  120. GetRenderCFrame(part)
  121. end
  122. function this:GetSubjectPosition()
  123. local result
  124. local camera = workspace.CurrentCamera
  125. local cameraSubject = camera and camera.CameraSubject
  126. if cameraSubject then
  127. if cameraSubject:IsA("VehicleSeat") then
  128. local subjectCFrame = GetRenderCFrame(cameraSubject)
  129. local offset = SEAT_OFFSET
  130. if VRService.VREnabled then
  131. offset = VR_SEAT_OFFSET
  132. end
  133. result = subjectCFrame.p + subjectCFrame:vectorToWorldSpace(offset)
  134. elseif cameraSubject:IsA("SkateboardPlatform") then
  135. local subjectCFrame = GetRenderCFrame(cameraSubject)
  136. result = subjectCFrame.p + SEAT_OFFSET
  137. elseif cameraSubject:IsA("BasePart") then
  138. local subjectCFrame = GetRenderCFrame(cameraSubject)
  139. result = subjectCFrame.p
  140. elseif cameraSubject:IsA("Model") then
  141. result = cameraSubject:GetModelCFrame().p
  142. elseif cameraSubject:IsA("Humanoid") then
  143. if cameraSubject:GetState() == Enum.HumanoidStateType.Dead and cameraSubject == this.lastSubject and VRService.VREnabled then
  144. result = this.lastSubjectPosition
  145. else
  146. local humanoidRootPart = cameraSubject.Torso
  147. if humanoidRootPart and humanoidRootPart:IsA("BasePart") then
  148. local subjectCFrame = GetRenderCFrame(humanoidRootPart)
  149. if cameraSubject.RigType == Enum.HumanoidRigType.R15 then
  150. result = subjectCFrame.p + subjectCFrame:vectorToWorldSpace(R15HeadHeight + cameraSubject.CameraOffset)
  151. else
  152. result = subjectCFrame.p + subjectCFrame:vectorToWorldSpace(HEAD_OFFSET + cameraSubject.CameraOffset)
  153. end
  154. end
  155. end
  156. end
  157. end
  158. this.lastSubject = cameraSubject
  159. this.lastSubjectPosition = result
  160. return result
  161. end
  162. function this:ResetCameraLook()
  163. end
  164. function this:GetCameraLook()
  165. return workspace.CurrentCamera and workspace.CurrentCamera.CoordinateFrame.lookVector or Vector3.new(0, 0, 1)
  166. end
  167. function this:GetCameraZoom()
  168. if this.currentZoom == nil then
  169. local player = PlayersService.LocalPlayer
  170. this.currentZoom = player and clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, 10) or 10
  171. end
  172. return this.currentZoom
  173. end
  174. function this:GetCameraActualZoom()
  175. local camera = workspace.CurrentCamera
  176. if camera then
  177. return camera.CoordinateFrame.p - camera.Focus.p.magnitude
  178. end
  179. end
  180. function this:GetCameraHeight()
  181. if not this:IsInFirstPerson() and VRService.VREnabled then
  182. local zoom = this:GetCameraZoom()
  183. return math.sin(VR_ANGLE) * zoom
  184. end
  185. return 0
  186. end
  187. function this:ViewSizeX()
  188. local result = 1024
  189. local camera = workspace.CurrentCamera
  190. if camera then
  191. result = camera.ViewportSize.X
  192. end
  193. return result
  194. end
  195. function this:ViewSizeY()
  196. local result = 768
  197. local camera = workspace.CurrentCamera
  198. if camera then
  199. result = camera.ViewportSize.Y
  200. end
  201. return result
  202. end
  203. function this:ScreenTranslationToAngle(translationVector)
  204. local screenX = this:ViewSizeX()
  205. local screenY = this:ViewSizeY()
  206. local xTheta = translationVector.x / screenX
  207. local yTheta = translationVector.y / screenY
  208. return Vector2.new(xTheta, yTheta)
  209. end
  210. function this:MouseTranslationToAngle(translationVector)
  211. local xTheta = translationVector.x / 1920
  212. local yTheta = translationVector.y / 1200
  213. return Vector2.new(xTheta, yTheta)
  214. end
  215. function this:RotateVector(startVector, xyRotateVector)
  216. local startCFrame = CFrame.new(Vector3.new(), startVector)
  217. local resultLookVector = CFrame.Angles(0, -xyRotateVector.x, 0) * startCFrame * CFrame.Angles(-xyRotateVector.y, 0, 0).lookVector
  218. return resultLookVector, Vector2.new(xyRotateVector.x, xyRotateVector.y)
  219. end
  220. function this:RotateCamera(startLook, xyRotateVector)
  221. if VRService.VREnabled then
  222. local yawRotatedVector, xyRotateVector = self:RotateVector(startLook, Vector2.new(xyRotateVector.x, 0))
  223. return Vector3.new(yawRotatedVector.x, 0, yawRotatedVector.z).unit, xyRotateVector
  224. else
  225. local startVertical = math.asin(startLook.y)
  226. local yTheta = clamp(-MAX_Y + startVertical, -MIN_Y + startVertical, xyRotateVector.y)
  227. return self:RotateVector(startLook, Vector2.new(xyRotateVector.x, yTheta))
  228. end
  229. end
  230. function this:IsInFirstPerson()
  231. return isFirstPerson
  232. end
  233. function this:UpdateMouseBehavior()
  234. if isFirstPerson or self:GetShiftLock() then
  235. pcall(function()
  236. GameSettings.RotationType = Enum.RotationType.CameraRelative
  237. end)
  238. if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
  239. UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
  240. end
  241. else
  242. pcall(function()
  243. GameSettings.RotationType = Enum.RotationType.MovementRelative
  244. end)
  245. if isRightMouseDown or isMiddleMouseDown then
  246. UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
  247. else
  248. UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  249. end
  250. end
  251. end
  252. function this:ZoomCamera(desiredZoom)
  253. local player = PlayersService.LocalPlayer
  254. if player then
  255. if player.CameraMode == Enum.CameraMode.LockFirstPerson then
  256. this.currentZoom = 0
  257. else
  258. this.currentZoom = clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, desiredZoom)
  259. end
  260. end
  261. isFirstPerson = self:GetCameraZoom() < 2
  262. ShiftLockController:SetIsInFirstPerson(isFirstPerson)
  263. self:UpdateMouseBehavior()
  264. return self:GetCameraZoom()
  265. end
  266. local rk4Integrator = function(position, velocity, t)
  267. local direction = velocity < 0 and -1 or 1
  268. local function acceleration(p, v)
  269. local accel = direction * math.max(1, p / 3.3 + 0.5)
  270. return accel
  271. end
  272. local p1 = position
  273. local v1 = velocity
  274. local a1 = acceleration(p1, v1)
  275. local p2 = p1 + v1 * (t / 2)
  276. local v2 = v1 + a1 * (t / 2)
  277. local a2 = acceleration(p2, v2)
  278. local p3 = p1 + v2 * (t / 2)
  279. local v3 = v1 + a2 * (t / 2)
  280. local a3 = acceleration(p3, v3)
  281. local p4 = p1 + v3 * t
  282. local v4 = v1 + a3 * t
  283. local a4 = acceleration(p4, v4)
  284. local positionResult = position + (v1 + 2 * v2 + 2 * v3 + v4) * (t / 6)
  285. local velocityResult = velocity + (a1 + 2 * a2 + 2 * a3 + a4) * (t / 6)
  286. return positionResult, velocityResult
  287. end
  288. function this:ZoomCameraBy(zoomScale)
  289. local zoom = this:GetCameraActualZoom()
  290. if zoom then
  291. if UserSettings():IsUserFeatureEnabled("UserBetterInertialScrolling") then
  292. zoom = rk4Integrator(zoom, zoomScale, 0.1)
  293. else
  294. zoom = rk4Integrator(zoom, zoomScale, 1)
  295. end
  296. self:ZoomCamera(zoom)
  297. end
  298. return self:GetCameraZoom()
  299. end
  300. function this:ZoomCameraFixedBy(zoomIncrement)
  301. return self:ZoomCamera(self:GetCameraZoom() + zoomIncrement)
  302. end
  303. function this:Update()
  304. end
  305. function this:ApplyVRTransform()
  306. if not VRService.VREnabled then
  307. return
  308. end
  309. local player = PlayersService.LocalPlayer
  310. if not player or not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") or not player.Character.HumanoidRootPart:FindFirstChild("RootJoint") then
  311. return
  312. end
  313. local camera = workspace.CurrentCamera
  314. local cameraSubject = camera.CameraSubject
  315. local isInVehicle = cameraSubject and cameraSubject:IsA("VehicleSeat")
  316. if this:IsInFirstPerson() and not isInVehicle then
  317. local vrFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
  318. local vrRotation = vrFrame - vrFrame.p
  319. local rootJoint = player.Character.HumanoidRootPart.RootJoint
  320. rootJoint.C0 = CFrame.new(vrRotation:vectorToObjectSpace(vrFrame.p)) * CFrame.new(0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0)
  321. else
  322. local rootJoint = player.Character.HumanoidRootPart.RootJoint
  323. rootJoint.C0 = CFrame.new(0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0)
  324. end
  325. end
  326. local vrRotationIntensityExists = true
  327. local lastVrRotationCheck = 0
  328. function this:ShouldUseVRRotation()
  329. if not VRService.VREnabled then
  330. return false
  331. end
  332. if not vrRotationIntensityExists and tick() - lastVrRotationCheck < 1 then
  333. return false
  334. end
  335. local success, vrRotationIntensity = pcall(function()
  336. return StarterGui:GetCore("VRRotationIntensity")
  337. end)
  338. vrRotationIntensityExists = success and vrRotationIntensity ~= nil
  339. lastVrRotationCheck = tick()
  340. return success and vrRotationIntensity ~= nil and vrRotationIntensity ~= "Smooth"
  341. end
  342. function this:GetVRRotationInput()
  343. local vrRotateSum = ZERO_VECTOR2
  344. local vrRotationIntensity = StarterGui:GetCore("VRRotationIntensity")
  345. local vrGamepadRotation = self.GamepadPanningCamera or ZERO_VECTOR2
  346. local delayExpired = tick() - lastVRRotation >= self:GetRepeatDelayValue(vrRotationIntensity)
  347. if math.abs(vrGamepadRotation.x) >= self:GetActivateValue() then
  348. if delayExpired or not vrRotateKeyCooldown[Enum.KeyCode.Thumbstick2] then
  349. local sign = 1
  350. if vrGamepadRotation.x < 0 then
  351. sign = -1
  352. end
  353. vrRotateSum = vrRotateSum + self:GetRotateAmountValue(vrRotationIntensity) * sign
  354. vrRotateKeyCooldown[Enum.KeyCode.Thumbstick2] = true
  355. end
  356. elseif math.abs(vrGamepadRotation.x) < self:GetActivateValue() - 0.1 then
  357. vrRotateKeyCooldown[Enum.KeyCode.Thumbstick2] = nil
  358. end
  359. if self.TurningLeft then
  360. if delayExpired or not vrRotateKeyCooldown[Enum.KeyCode.Left] then
  361. vrRotateSum = vrRotateSum - self:GetRotateAmountValue(vrRotationIntensity)
  362. vrRotateKeyCooldown[Enum.KeyCode.Left] = true
  363. end
  364. else
  365. vrRotateKeyCooldown[Enum.KeyCode.Left] = nil
  366. end
  367. if self.TurningRight then
  368. if delayExpired or not vrRotateKeyCooldown[Enum.KeyCode.Right] then
  369. vrRotateSum = vrRotateSum + self:GetRotateAmountValue(vrRotationIntensity)
  370. vrRotateKeyCooldown[Enum.KeyCode.Right] = true
  371. end
  372. else
  373. vrRotateKeyCooldown[Enum.KeyCode.Right] = nil
  374. end
  375. if vrRotateSum ~= ZERO_VECTOR2 then
  376. lastVRRotation = tick()
  377. end
  378. return vrRotateSum
  379. end
  380. local cameraTranslationConstraints = Vector3.new(1, 1, 1)
  381. local humanoidJumpOrigin, trackingHumanoid
  382. local cameraFrozen = false
  383. local subjectStateChangedConn, cameraChangedConn, workspaceChangedConn, humanoidChildAddedConn, humanoidChildRemovedConn
  384. local function cancelCameraFreeze(keepConstraints)
  385. if not keepConstraints then
  386. cameraTranslationConstraints = Vector3.new(cameraTranslationConstraints.x, 1, cameraTranslationConstraints.z)
  387. end
  388. if cameraFrozen then
  389. trackingHumanoid = nil
  390. cameraFrozen = false
  391. end
  392. end
  393. local function startCameraFreeze(subjectPosition, humanoidToTrack)
  394. if not cameraFrozen then
  395. humanoidJumpOrigin = subjectPosition
  396. trackingHumanoid = humanoidToTrack
  397. cameraTranslationConstraints = Vector3.new(cameraTranslationConstraints.x, 0, cameraTranslationConstraints.z)
  398. cameraFrozen = true
  399. end
  400. end
  401. local function rescaleCameraOffset(newScaleFactor)
  402. R15HeadHeight = R15_HEAD_OFFSET * newScaleFactor
  403. end
  404. local function onHumanoidSubjectChildAdded(child)
  405. if child.Name == "BodyHeightScale" and child:IsA("NumberValue") then
  406. if heightScaleChangedConn then
  407. heightScaleChangedConn:disconnect()
  408. end
  409. heightScaleChangedConn = child.Changed:connect(rescaleCameraOffset)
  410. rescaleCameraOffset(child.Value)
  411. end
  412. end
  413. local function onHumanoidSubjectChildRemoved(child)
  414. if child.Name == "BodyHeightScale" then
  415. rescaleCameraOffset(1)
  416. if heightScaleChangedConn then
  417. heightScaleChangedConn:disconnect()
  418. heightScaleChangedConn = nil
  419. end
  420. end
  421. end
  422. local function onNewCameraSubject()
  423. if subjectStateChangedConn then
  424. subjectStateChangedConn:disconnect()
  425. subjectStateChangedConn = nil
  426. end
  427. if humanoidChildAddedConn then
  428. humanoidChildAddedConn:disconnect()
  429. humanoidChildAddedConn = nil
  430. end
  431. if humanoidChildRemovedConn then
  432. humanoidChildRemovedConn:disconnect()
  433. humanoidChildRemovedConn = nil
  434. end
  435. if heightScaleChangedConn then
  436. heightScaleChangedConn:disconnect()
  437. heightScaleChangedConn = nil
  438. end
  439. local humanoid = workspace.CurrentCamera and workspace.CurrentCamera.CameraSubject
  440. if trackingHumanoid ~= humanoid then
  441. cancelCameraFreeze()
  442. end
  443. if humanoid and humanoid:IsA("Humanoid") then
  444. humanoidChildAddedConn = humanoid.ChildAdded:connect(onHumanoidSubjectChildAdded)
  445. humanoidChildRemovedConn = humanoid.ChildRemoved:connect(onHumanoidSubjectChildRemoved)
  446. for _, child in pairs(humanoid:GetChildren()) do
  447. onHumanoidSubjectChildAdded(child)
  448. end
  449. subjectStateChangedConn = humanoid.StateChanged:connect(function(oldState, newState)
  450. if newState == Enum.HumanoidStateType.Jumping and not this:IsInFirstPerson() and VRService.VREnabled then
  451. startCameraFreeze(this:GetSubjectPosition(), humanoid)
  452. elseif newState ~= Enum.HumanoidStateType.Jumping and newState ~= Enum.HumanoidStateType.Freefall then
  453. cancelCameraFreeze(true)
  454. end
  455. end)
  456. end
  457. end
  458. local function onCameraChanged(prop)
  459. if prop == "CameraSubject" then
  460. onNewCameraSubject()
  461. end
  462. end
  463. local function onCurrentCameraChanged()
  464. if cameraChangedConn then
  465. cameraChangedConn:disconnect()
  466. cameraChangedConn = nil
  467. end
  468. local camera = workspace.CurrentCamera
  469. if camera then
  470. camera.Changed:connect(onCameraChanged)
  471. onCameraChanged("CameraSubject")
  472. end
  473. end
  474. function this:GetVRFocus(subjectPosition, timeDelta)
  475. local newFocus
  476. local camera = workspace.CurrentCamera
  477. local lastFocus = self.LastCameraFocus or subjectPosition
  478. if not cameraFrozen then
  479. cameraTranslationConstraints = Vector3.new(cameraTranslationConstraints.x, math.min(1, cameraTranslationConstraints.y + 0.42 * timeDelta), cameraTranslationConstraints.z)
  480. end
  481. if cameraFrozen and humanoidJumpOrigin and humanoidJumpOrigin.y > lastFocus.y then
  482. newFocus = CFrame.new(Vector3.new(subjectPosition.x, math.min(humanoidJumpOrigin.y, lastFocus.y + 5 * timeDelta), subjectPosition.z))
  483. else
  484. newFocus = CFrame.new(Vector3.new(subjectPosition.x, lastFocus.y, subjectPosition.z):lerp(subjectPosition, cameraTranslationConstraints.y))
  485. end
  486. if cameraFrozen then
  487. if self:IsInFirstPerson() then
  488. cancelCameraFreeze()
  489. end
  490. if humanoidJumpOrigin and subjectPosition.y < humanoidJumpOrigin.y - 0.5 then
  491. cancelCameraFreeze()
  492. end
  493. end
  494. return newFocus
  495. end
  496. local startPos, lastPos, panBeginLook
  497. local fingerTouches = {}
  498. local NumUnsunkTouches = 0
  499. local StartingDiff, pinchBeginZoom
  500. this.ZoomEnabled = true
  501. this.PanEnabled = true
  502. this.KeyPanEnabled = true
  503. local function OnTouchBegan(input, processed)
  504. fingerTouches[input] = processed
  505. if not processed then
  506. NumUnsunkTouches = NumUnsunkTouches + 1
  507. end
  508. end
  509. local function OnTouchChanged(input, processed)
  510. if fingerTouches[input] == nil then
  511. fingerTouches[input] = processed
  512. if not processed then
  513. NumUnsunkTouches = NumUnsunkTouches + 1
  514. end
  515. end
  516. if NumUnsunkTouches == 1 then
  517. if fingerTouches[input] == false then
  518. panBeginLook = panBeginLook or this:GetCameraLook()
  519. startPos = startPos or input.Position
  520. lastPos = lastPos or startPos
  521. this.UserPanningTheCamera = true
  522. local delta = input.Position - lastPos
  523. if this.PanEnabled then
  524. local desiredXYVector = this:ScreenTranslationToAngle(delta) * TOUCH_SENSITIVTY
  525. this.RotateInput = this.RotateInput + desiredXYVector
  526. end
  527. lastPos = input.Position
  528. end
  529. else
  530. panBeginLook = nil
  531. startPos = nil
  532. lastPos = nil
  533. this.UserPanningTheCamera = false
  534. end
  535. if NumUnsunkTouches == 2 then
  536. local unsunkTouches = {}
  537. for touch, wasSunk in pairs(fingerTouches) do
  538. if not wasSunk then
  539. table.insert(unsunkTouches, touch)
  540. end
  541. end
  542. if #unsunkTouches == 2 then
  543. local difference = unsunkTouches[1].Position - unsunkTouches[2].Position.magnitude
  544. if StartingDiff and pinchBeginZoom then
  545. local scale = difference / math.max(0.01, StartingDiff)
  546. local clampedScale = clamp(0.1, 10, scale)
  547. if this.ZoomEnabled then
  548. this:ZoomCamera(pinchBeginZoom / clampedScale)
  549. end
  550. else
  551. StartingDiff = difference
  552. pinchBeginZoom = this:GetCameraActualZoom()
  553. end
  554. end
  555. else
  556. StartingDiff = nil
  557. pinchBeginZoom = nil
  558. end
  559. end
  560. local function OnTouchEnded(input, processed)
  561. if fingerTouches[input] == false then
  562. if NumUnsunkTouches == 1 then
  563. panBeginLook = nil
  564. startPos = nil
  565. lastPos = nil
  566. this.UserPanningTheCamera = false
  567. elseif NumUnsunkTouches == 2 then
  568. StartingDiff = nil
  569. pinchBeginZoom = nil
  570. end
  571. end
  572. if fingerTouches[input] ~= nil and fingerTouches[input] == false then
  573. NumUnsunkTouches = NumUnsunkTouches - 1
  574. end
  575. fingerTouches[input] = nil
  576. end
  577. local function OnMousePanButtonPressed(input, processed)
  578. if processed then
  579. return
  580. end
  581. this:UpdateMouseBehavior()
  582. panBeginLook = panBeginLook or this:GetCameraLook()
  583. startPos = startPos or input.Position
  584. lastPos = lastPos or startPos
  585. this.UserPanningTheCamera = true
  586. end
  587. local function OnMousePanButtonReleased(input, processed)
  588. this:UpdateMouseBehavior()
  589. if not isRightMouseDown and not isMiddleMouseDown then
  590. panBeginLook = nil
  591. startPos = nil
  592. lastPos = nil
  593. this.UserPanningTheCamera = false
  594. end
  595. end
  596. local function OnMouse2Down(input, processed)
  597. if processed then
  598. return
  599. end
  600. isRightMouseDown = true
  601. OnMousePanButtonPressed(input, processed)
  602. end
  603. local function OnMouse2Up(input, processed)
  604. isRightMouseDown = false
  605. OnMousePanButtonReleased(input, processed)
  606. end
  607. local function OnMouse3Down(input, processed)
  608. if processed then
  609. return
  610. end
  611. isMiddleMouseDown = true
  612. OnMousePanButtonPressed(input, processed)
  613. end
  614. local function OnMouse3Up(input, processed)
  615. isMiddleMouseDown = false
  616. OnMousePanButtonReleased(input, processed)
  617. end
  618. local function OnMouseMoved(input, processed)
  619. if not hasGameLoaded and VRService.VREnabled then
  620. return
  621. end
  622. if startPos and lastPos and panBeginLook then
  623. local currPos = lastPos + input.Delta
  624. local totalTrans = currPos - startPos
  625. if this.PanEnabled then
  626. local desiredXYVector = this:MouseTranslationToAngle(input.Delta) * MOUSE_SENSITIVITY
  627. this.RotateInput = this.RotateInput + desiredXYVector
  628. end
  629. lastPos = currPos
  630. elseif (this:IsInFirstPerson() or this:GetShiftLock()) and this.PanEnabled then
  631. local desiredXYVector = this:MouseTranslationToAngle(input.Delta) * MOUSE_SENSITIVITY
  632. this.RotateInput = this.RotateInput + desiredXYVector
  633. end
  634. end
  635. local function OnMouseWheel(input, processed)
  636. if not hasGameLoaded and VRService.VREnabled then
  637. return
  638. end
  639. if not processed and this.ZoomEnabled then
  640. if UserSettings():IsUserFeatureEnabled("UserBetterInertialScrolling") then
  641. this:ZoomCameraBy(-input.Position.Z / 15)
  642. else
  643. this:ZoomCameraBy(clamp(-1, 1, -input.Position.Z) * 1.4)
  644. end
  645. end
  646. end
  647. local round = function(num)
  648. return math.floor(num + 0.5)
  649. end
  650. local eight2Pi = math.pi / 4
  651. local function rotateVectorByAngleAndRound(camLook, rotateAngle, roundAmount)
  652. if camLook ~= Vector3.new(0, 0, 0) then
  653. camLook = camLook.unit
  654. local currAngle = math.atan2(camLook.z, camLook.x)
  655. local newAngle = round((math.atan2(camLook.z, camLook.x) + rotateAngle) / roundAmount) * roundAmount
  656. return newAngle - currAngle
  657. end
  658. return 0
  659. end
  660. local function OnKeyDown(input, processed)
  661. if not hasGameLoaded and VRService.VREnabled then
  662. return
  663. end
  664. if processed then
  665. return
  666. end
  667. if this.ZoomEnabled then
  668. if input.KeyCode == Enum.KeyCode.I then
  669. this:ZoomCameraBy(-5)
  670. elseif input.KeyCode == Enum.KeyCode.O then
  671. this:ZoomCameraBy(5)
  672. end
  673. end
  674. if panBeginLook == nil and this.KeyPanEnabled then
  675. if input.KeyCode == Enum.KeyCode.Left then
  676. this.TurningLeft = true
  677. elseif input.KeyCode == Enum.KeyCode.Right then
  678. this.TurningRight = true
  679. elseif input.KeyCode == Enum.KeyCode.Comma then
  680. local angle = rotateVectorByAngleAndRound(this:GetCameraLook() * Vector3.new(1, 0, 1), -eight2Pi * 0.75, eight2Pi)
  681. if angle ~= 0 then
  682. this.RotateInput = this.RotateInput + Vector2.new(angle, 0)
  683. this.LastUserPanCamera = tick()
  684. this.LastCameraTransform = nil
  685. end
  686. elseif input.KeyCode == Enum.KeyCode.Period then
  687. local angle = rotateVectorByAngleAndRound(this:GetCameraLook() * Vector3.new(1, 0, 1), eight2Pi * 0.75, eight2Pi)
  688. if angle ~= 0 then
  689. this.RotateInput = this.RotateInput + Vector2.new(angle, 0)
  690. this.LastUserPanCamera = tick()
  691. this.LastCameraTransform = nil
  692. end
  693. elseif input.KeyCode == Enum.KeyCode.PageUp then
  694. this.RotateInput = this.RotateInput + Vector2.new(0, math.rad(15))
  695. this.LastCameraTransform = nil
  696. elseif input.KeyCode == Enum.KeyCode.PageDown then
  697. this.RotateInput = this.RotateInput + Vector2.new(0, math.rad(-15))
  698. this.LastCameraTransform = nil
  699. end
  700. end
  701. end
  702. local function OnKeyUp(input, processed)
  703. if input.KeyCode == Enum.KeyCode.Left then
  704. this.TurningLeft = false
  705. elseif input.KeyCode == Enum.KeyCode.Right then
  706. this.TurningRight = false
  707. end
  708. end
  709. local lastThumbstickRotate
  710. local numOfSeconds = 0.7
  711. local currentSpeed = 0
  712. local maxSpeed = 6
  713. local vrMaxSpeed = 4
  714. local thumbstickSensitivity = 1
  715. local lastThumbstickPos = Vector2.new(0, 0)
  716. local ySensitivity = 0.65
  717. local lastVelocity
  718. local k = 0.35
  719. local lowerK = 0.8
  720. local function SCurveTranform(t)
  721. t = clamp(-1, 1, t)
  722. if t >= 0 then
  723. return k * t / (k - t + 1)
  724. end
  725. return -(lowerK * -t / (lowerK + t + 1))
  726. end
  727. local DEADZONE = 0.1
  728. local function toSCurveSpace(t)
  729. return (1 + DEADZONE) * (2 * math.abs(t) - 1) - DEADZONE
  730. end
  731. local fromSCurveSpace = function(t)
  732. return t / 2 + 0.5
  733. end
  734. local function gamepadLinearToCurve(thumbstickPosition)
  735. local function onAxis(axisValue)
  736. local sign = 1
  737. if axisValue < 0 then
  738. sign = -1
  739. end
  740. local point = fromSCurveSpace(SCurveTranform(toSCurveSpace(math.abs(axisValue))))
  741. point = point * sign
  742. return clamp(-1, 1, point)
  743. end
  744. return Vector2.new(onAxis(thumbstickPosition.x), onAxis(thumbstickPosition.y))
  745. end
  746. function this:UpdateGamepad()
  747. local gamepadPan = this.GamepadPanningCamera
  748. if gamepadPan and (hasGameLoaded or not VRService.VREnabled) then
  749. gamepadPan = gamepadLinearToCurve(gamepadPan)
  750. local currentTime = tick()
  751. if gamepadPan.X ~= 0 or gamepadPan.Y ~= 0 then
  752. this.userPanningTheCamera = true
  753. elseif gamepadPan == Vector2.new(0, 0) then
  754. lastThumbstickRotate = nil
  755. if lastThumbstickPos == Vector2.new(0, 0) then
  756. currentSpeed = 0
  757. end
  758. end
  759. local finalConstant = 0
  760. if lastThumbstickRotate then
  761. if VRService.VREnabled then
  762. currentSpeed = vrMaxSpeed
  763. else
  764. local elapsedTime = (currentTime - lastThumbstickRotate) * 10
  765. currentSpeed = currentSpeed + maxSpeed * (elapsedTime * elapsedTime / numOfSeconds)
  766. if currentSpeed > maxSpeed then
  767. currentSpeed = maxSpeed
  768. end
  769. if lastVelocity then
  770. local velocity = (gamepadPan - lastThumbstickPos) / (currentTime - lastThumbstickRotate)
  771. local velocityDeltaMag = velocity - lastVelocity.magnitude
  772. if velocityDeltaMag > 12 then
  773. currentSpeed = currentSpeed * (20 / velocityDeltaMag)
  774. if currentSpeed > maxSpeed then
  775. currentSpeed = maxSpeed
  776. end
  777. end
  778. end
  779. end
  780. finalConstant = thumbstickSensitivity * currentSpeed
  781. lastVelocity = (gamepadPan - lastThumbstickPos) / (currentTime - lastThumbstickRotate)
  782. end
  783. lastThumbstickPos = gamepadPan
  784. lastThumbstickRotate = currentTime
  785. return Vector2.new(gamepadPan.X * finalConstant, gamepadPan.Y * finalConstant * ySensitivity)
  786. end
  787. return Vector2.new(0, 0)
  788. end
  789. local InputBeganConn, InputChangedConn, InputEndedConn, MenuOpenedConn, ShiftLockToggleConn, GamepadConnectedConn, GamepadDisconnectedConn
  790. function this:DisconnectInputEvents()
  791. if InputBeganConn then
  792. InputBeganConn:disconnect()
  793. InputBeganConn = nil
  794. end
  795. if InputChangedConn then
  796. InputChangedConn:disconnect()
  797. InputChangedConn = nil
  798. end
  799. if InputEndedConn then
  800. InputEndedConn:disconnect()
  801. InputEndedConn = nil
  802. end
  803. if MenuOpenedConn then
  804. MenuOpenedConn:disconnect()
  805. MenuOpenedConn = nil
  806. end
  807. if ShiftLockToggleConn then
  808. ShiftLockToggleConn:disconnect()
  809. ShiftLockToggleConn = nil
  810. end
  811. if GamepadConnectedConn then
  812. GamepadConnectedConn:disconnect()
  813. GamepadConnectedConn = nil
  814. end
  815. if GamepadDisconnectedConn then
  816. GamepadDisconnectedConn:disconnect()
  817. GamepadDisconnectedConn = nil
  818. end
  819. if subjectStateChangedConn then
  820. subjectStateChangedConn:disconnect()
  821. subjectStateChangedConn = nil
  822. end
  823. if cameraChangedConn then
  824. cameraChangedConn:disconnect()
  825. cameraChangedConn = nil
  826. end
  827. if workspaceChangedConn then
  828. workspaceChangedConn:disconnect()
  829. workspaceChangedConn = nil
  830. end
  831. this.TurningLeft = false
  832. this.TurningRight = false
  833. this.LastCameraTransform = nil
  834. self.LastSubjectCFrame = nil
  835. this.UserPanningTheCamera = false
  836. this.RotateInput = Vector2.new()
  837. this.GamepadPanningCamera = Vector2.new(0, 0)
  838. startPos = nil
  839. lastPos = nil
  840. panBeginLook = nil
  841. isRightMouseDown = false
  842. isMiddleMouseDown = false
  843. fingerTouches = {}
  844. NumUnsunkTouches = 0
  845. StartingDiff = nil
  846. pinchBeginZoom = nil
  847. if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
  848. UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  849. end
  850. end
  851. function this:ResetInputStates()
  852. isRightMouseDown = false
  853. isMiddleMouseDown = false
  854. OnMousePanButtonReleased()
  855. if UserInputService.TouchEnabled then
  856. for inputObject, value in pairs(fingerTouches) do
  857. fingerTouches[inputObject] = nil
  858. end
  859. panBeginLook = nil
  860. startPos = nil
  861. lastPos = nil
  862. this.UserPanningTheCamera = false
  863. StartingDiff = nil
  864. pinchBeginZoom = nil
  865. NumUnsunkTouches = 0
  866. end
  867. end
  868. function this:ConnectInputEvents()
  869. InputBeganConn = UserInputService.InputBegan:connect(function(input, processed)
  870. if input.UserInputType == Enum.UserInputType.Touch then
  871. OnTouchBegan(input, processed)
  872. elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
  873. OnMouse2Down(input, processed)
  874. elseif input.UserInputType == Enum.UserInputType.MouseButton3 then
  875. OnMouse3Down(input, processed)
  876. end
  877. if input.UserInputType == Enum.UserInputType.Keyboard then
  878. OnKeyDown(input, processed)
  879. end
  880. end)
  881. InputChangedConn = UserInputService.InputChanged:connect(function(input, processed)
  882. if input.UserInputType == Enum.UserInputType.Touch then
  883. OnTouchChanged(input, processed)
  884. elseif input.UserInputType == Enum.UserInputType.MouseMovement then
  885. OnMouseMoved(input, processed)
  886. elseif input.UserInputType == Enum.UserInputType.MouseWheel then
  887. OnMouseWheel(input, processed)
  888. end
  889. end)
  890. InputEndedConn = UserInputService.InputEnded:connect(function(input, processed)
  891. if input.UserInputType == Enum.UserInputType.Touch then
  892. OnTouchEnded(input, processed)
  893. elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
  894. OnMouse2Up(input, processed)
  895. elseif input.UserInputType == Enum.UserInputType.MouseButton3 then
  896. OnMouse3Up(input, processed)
  897. end
  898. if input.UserInputType == Enum.UserInputType.Keyboard then
  899. OnKeyUp(input, processed)
  900. end
  901. end)
  902. MenuOpenedConn = GuiService.MenuOpened:connect(function()
  903. this:ResetInputStates()
  904. end)
  905. workspaceChangedConn = workspace.Changed:connect(function(prop)
  906. if prop == "CurrentCamera" then
  907. onCurrentCameraChanged()
  908. end
  909. end)
  910. onCurrentCameraChanged()
  911. ShiftLockToggleConn = ShiftLockController.OnShiftLockToggled.Event:connect(function()
  912. this:UpdateMouseBehavior()
  913. end)
  914. this.RotateInput = Vector2.new()
  915. local activateGamepad
  916. local function assignActivateGamepad()
  917. local connectedGamepads = UserInputService:GetConnectedGamepads()
  918. if #connectedGamepads > 0 then
  919. for i = 1, #connectedGamepads do
  920. if activateGamepad == nil then
  921. activateGamepad = connectedGamepads[i]
  922. elseif connectedGamepads[i].Value < activateGamepad.Value then
  923. activateGamepad = connectedGamepads[i]
  924. end
  925. end
  926. end
  927. if activateGamepad == nil then
  928. activateGamepad = Enum.UserInputType.Gamepad1
  929. end
  930. end
  931. GamepadConnectedConn = UserInputService.GamepadDisconnected:connect(function(gamepadEnum)
  932. if activateGamepad ~= gamepadEnum then
  933. return
  934. end
  935. activateGamepad = nil
  936. assignActivateGamepad()
  937. end)
  938. GamepadDisconnectedConn = UserInputService.GamepadConnected:connect(function(gamepadEnum)
  939. if activateGamepad == nil then
  940. assignActivateGamepad()
  941. end
  942. end)
  943. local function getGamepadPan(name, state, input)
  944. if input.UserInputType == activateGamepad and input.KeyCode == Enum.KeyCode.Thumbstick2 then
  945. if state == Enum.UserInputState.Cancel then
  946. this.GamepadPanningCamera = Vector2.new(0, 0)
  947. return
  948. end
  949. local inputVector = Vector2.new(input.Position.X, -input.Position.Y)
  950. if inputVector.magnitude > THUMBSTICK_DEADZONE then
  951. this.GamepadPanningCamera = Vector2.new(input.Position.X, -input.Position.Y)
  952. else
  953. this.GamepadPanningCamera = Vector2.new(0, 0)
  954. end
  955. end
  956. end
  957. local function doGamepadZoom(name, state, input)
  958. if input.UserInputType == activateGamepad and input.KeyCode == Enum.KeyCode.ButtonR3 and state == Enum.UserInputState.Begin and this.ZoomEnabled then
  959. if this.currentZoom > 0.5 then
  960. this:ZoomCamera(0)
  961. else
  962. this:ZoomCamera(10)
  963. end
  964. end
  965. end
  966. ContextActionService:BindAction("RootCamGamepadPan", getGamepadPan, false, Enum.KeyCode.Thumbstick2)
  967. ContextActionService:BindAction("RootCamGamepadZoom", doGamepadZoom, false, Enum.KeyCode.ButtonR3)
  968. assignActivateGamepad()
  969. self:UpdateMouseBehavior()
  970. end
  971. function this:SetEnabled(newState)
  972. if newState ~= self.Enabled then
  973. self.Enabled = newState
  974. if self.Enabled then
  975. self:ConnectInputEvents()
  976. self.cframe = workspace.CurrentCamera.CoordinateFrame
  977. else
  978. self:DisconnectInputEvents()
  979. end
  980. end
  981. end
  982. local function OnPlayerAdded(player)
  983. player.Changed:connect(function(prop)
  984. if this.Enabled and (prop == "CameraMode" or prop == "CameraMaxZoomDistance" or prop == "CameraMinZoomDistance") then
  985. this:ZoomCameraFixedBy(0)
  986. end
  987. end)
  988. local function OnCharacterAdded(newCharacter)
  989. if not FFlagSpawnLocationFixEnabled then
  990. this:ZoomCamera(12.5)
  991. end
  992. local humanoid = findPlayerHumanoid(player)
  993. local start = tick()
  994. while tick() - start < 0.3 and (humanoid == nil or humanoid.Torso == nil) do
  995. wait()
  996. humanoid = findPlayerHumanoid(player)
  997. end
  998. if not FFlagSpawnLocationFixEnabled then
  999. wait()
  1000. end
  1001. if humanoid and humanoid.Torso and player.Character == newCharacter then
  1002. local newDesiredLook = humanoid.Torso.CFrame.lookVector - Vector3.new(0, 0.23, 0).unit
  1003. local horizontalShift = findAngleBetweenXZVectors(newDesiredLook, this:GetCameraLook())
  1004. local vertShift = math.asin(this:GetCameraLook().y) - math.asin(newDesiredLook.y)
  1005. if not IsFinite(horizontalShift) then
  1006. horizontalShift = 0
  1007. end
  1008. if not IsFinite(vertShift) then
  1009. vertShift = 0
  1010. end
  1011. this.RotateInput = Vector2.new(horizontalShift, vertShift)
  1012. this.LastCameraTransform = nil
  1013. end
  1014. if FFlagSpawnLocationFixEnabled then
  1015. wait()
  1016. this:ZoomCamera(12.5)
  1017. end
  1018. end
  1019. player.CharacterAdded:connect(function(character)
  1020. if this.Enabled or SetCameraOnSpawn then
  1021. OnCharacterAdded(character)
  1022. SetCameraOnSpawn = false
  1023. end
  1024. end)
  1025. if player.Character then
  1026. spawn(function()
  1027. OnCharacterAdded(player.Character)
  1028. end)
  1029. end
  1030. end
  1031. if PlayersService.LocalPlayer then
  1032. OnPlayerAdded(PlayersService.LocalPlayer)
  1033. end
  1034. PlayersService.ChildAdded:connect(function(child)
  1035. if child and PlayersService.LocalPlayer == child then
  1036. OnPlayerAdded(PlayersService.LocalPlayer)
  1037. end
  1038. end)
  1039. local function OnGameLoaded()
  1040. hasGameLoaded = true
  1041. end
  1042. spawn(function()
  1043. if game:IsLoaded() then
  1044. OnGameLoaded()
  1045. else
  1046. game.Loaded:wait()
  1047. OnGameLoaded()
  1048. end
  1049. end)
  1050. local function FakeZoomCamera(n)
  1051. this:ZoomCamera(n)
  1052. end
  1053. script.Parent.Parent:WaitForChild("ZoomCamera").Event:connect(FakeZoomCamera)
  1054. return this
  1055. end
  1056. return CreateCamera
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement