Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. local PlayersService = game:GetService("Players")
  2. local VRService = game:GetService("VRService")
  3. local RootCameraCreator = require(script.Parent)
  4. local UP_VECTOR = Vector3.new(0, 1, 0)
  5. local XZ_VECTOR = Vector3.new(1, 0, 1)
  6. local ZERO_VECTOR2 = Vector2.new(0, 0)
  7. local VR_PITCH_FRACTION = 0.25
  8. local Vector3_new = Vector3.new
  9. local CFrame_new = CFrame.new
  10. local math_min = math.min
  11. local math_max = math.max
  12. local math_atan2 = math.atan2
  13. local math_rad = math.rad
  14. local math_abs = math.abs
  15. local clamp = function(low, high, num)
  16. return high < num and high or num < low and low or num
  17. end
  18. local IsFinite = function(num)
  19. return num == num and num ~= 1 / 0 and num ~= -1 / 0
  20. end
  21. local function IsFiniteVector3(vec3)
  22. return IsFinite(vec3.x) and IsFinite(vec3.y) and IsFinite(vec3.z)
  23. end
  24. local function findAngleBetweenXZVectors(vec2, vec1)
  25. return math_atan2(vec1.X * vec2.Z - vec1.Z * vec2.X, vec1.X * vec2.X + vec1.Z * vec2.Z)
  26. end
  27. local function CreateClassicCamera()
  28. local module = RootCameraCreator()
  29. local tweenAcceleration = math_rad(220)
  30. local tweenSpeed = math_rad(0)
  31. local tweenMaxSpeed = math_rad(250)
  32. local timeBeforeAutoRotate = 2
  33. local lastUpdate = tick()
  34. module.LastUserPanCamera = tick()
  35. function module:Update()
  36. module:ProcessTweens()
  37. local now = tick()
  38. local timeDelta = now - lastUpdate
  39. local userPanningTheCamera = self.UserPanningTheCamera == true
  40. local camera = workspace.CurrentCamera
  41. local player = PlayersService.LocalPlayer
  42. local humanoid = self:GetHumanoid()
  43. local cameraSubject = camera and camera.CameraSubject
  44. local isInVehicle = cameraSubject and cameraSubject:IsA("VehicleSeat")
  45. local isOnASkateboard = cameraSubject and cameraSubject:IsA("SkateboardPlatform")
  46. if lastUpdate == nil or now - lastUpdate > 1 then
  47. module:ResetCameraLook()
  48. self.LastCameraTransform = nil
  49. end
  50. if lastUpdate then
  51. local gamepadRotation = self:UpdateGamepad()
  52. if self:ShouldUseVRRotation() then
  53. self.RotateInput = self.RotateInput + self:GetVRRotationInput()
  54. else
  55. local delta = math_min(0.1, now - lastUpdate)
  56. if gamepadRotation ~= ZERO_VECTOR2 then
  57. userPanningTheCamera = true
  58. self.RotateInput = self.RotateInput + gamepadRotation * delta
  59. end
  60. local angle = 0
  61. if not isInVehicle and not isOnASkateboard then
  62. angle = angle + (self.TurningLeft and -120 or 0)
  63. angle = angle + (self.TurningRight and 120 or 0)
  64. end
  65. if angle ~= 0 then
  66. self.RotateInput = self.RotateInput + Vector2.new(math_rad(angle * delta), 0)
  67. userPanningTheCamera = true
  68. end
  69. end
  70. end
  71. if userPanningTheCamera then
  72. tweenSpeed = 0
  73. module.LastUserPanCamera = tick()
  74. end
  75. local userRecentlyPannedCamera = now - module.LastUserPanCamera < timeBeforeAutoRotate
  76. local subjectPosition = self:GetSubjectPosition()
  77. if subjectPosition and player and camera then
  78. local zoom = self:GetCameraZoom()
  79. if zoom < 0.5 then
  80. zoom = 0.5
  81. end
  82. if self:GetShiftLock() and not self:IsInFirstPerson() then
  83. local newLookVector = self:RotateCamera(self:GetCameraLook(), self.RotateInput)
  84. local offset = newLookVector * XZ_VECTOR:Cross(UP_VECTOR).unit * 1.75
  85. if IsFiniteVector3(offset) then
  86. subjectPosition = subjectPosition + offset
  87. end
  88. elseif not userPanningTheCamera and self.LastCameraTransform then
  89. local isInFirstPerson = self:IsInFirstPerson()
  90. if (isInVehicle or isOnASkateboard) and lastUpdate and humanoid and humanoid.Torso then
  91. if isInFirstPerson then
  92. if self.LastSubjectCFrame and (isInVehicle or isOnASkateboard) and cameraSubject:IsA("BasePart") then
  93. local y = -findAngleBetweenXZVectors(self.LastSubjectCFrame.lookVector, cameraSubject.CFrame.lookVector)
  94. if IsFinite(y) then
  95. self.RotateInput = self.RotateInput + Vector2.new(y, 0)
  96. end
  97. tweenSpeed = 0
  98. end
  99. elseif not userRecentlyPannedCamera then
  100. local forwardVector = humanoid.Torso.CFrame.lookVector
  101. if isOnASkateboard then
  102. forwardVector = cameraSubject.CFrame.lookVector
  103. end
  104. tweenSpeed = clamp(0, tweenMaxSpeed, tweenSpeed + tweenAcceleration * timeDelta)
  105. local percent = clamp(0, 1, tweenSpeed * timeDelta)
  106. if self:IsInFirstPerson() then
  107. percent = 1
  108. end
  109. local y = findAngleBetweenXZVectors(forwardVector, self:GetCameraLook())
  110. if IsFinite(y) and math_abs(y) > 1.0E-4 then
  111. self.RotateInput = self.RotateInput + Vector2.new(y * percent, 0)
  112. end
  113. end
  114. end
  115. end
  116. local VREnabled = VRService.VREnabled
  117. if not VREnabled or not self:GetVRFocus(subjectPosition, timeDelta) then
  118. end
  119. camera.Focus = CFrame_new(subjectPosition)
  120. local cameraFocusP = camera.Focus.p
  121. if VREnabled and not self:IsInFirstPerson() then
  122. local cameraHeight = self:GetCameraHeight()
  123. local vecToSubject = subjectPosition - camera.CFrame.p
  124. local distToSubject = vecToSubject.magnitude
  125. if zoom < distToSubject or self.RotateInput.x ~= 0 then
  126. local desiredDist = math_min(distToSubject, zoom)
  127. vecToSubject = self:RotateCamera(vecToSubject.unit * XZ_VECTOR, Vector2.new(self.RotateInput.x, 0)) * desiredDist
  128. local newPos = cameraFocusP - vecToSubject
  129. local desiredLookDir = camera.CFrame.lookVector
  130. if self.RotateInput.x ~= 0 then
  131. desiredLookDir = vecToSubject
  132. end
  133. local lookAt = Vector3.new(newPos.x + desiredLookDir.x, newPos.y, newPos.z + desiredLookDir.z)
  134. self.RotateInput = ZERO_VECTOR2
  135. camera.CFrame = CFrame_new(newPos, lookAt) + Vector3_new(0, cameraHeight, 0)
  136. end
  137. else
  138. local newLookVector = self:RotateCamera(self:GetCameraLook(), self.RotateInput)
  139. self.RotateInput = ZERO_VECTOR2
  140. camera.CFrame = CFrame_new(cameraFocusP - zoom * newLookVector, cameraFocusP)
  141. end
  142. self.LastCameraTransform = camera.CFrame
  143. self.LastCameraFocus = camera.Focus
  144. if (isInVehicle or isOnASkateboard) and cameraSubject:IsA("BasePart") then
  145. self.LastSubjectCFrame = cameraSubject.CFrame
  146. else
  147. self.LastSubjectCFrame = nil
  148. end
  149. end
  150. lastUpdate = now
  151. end
  152. return module
  153. end
  154. return CreateClassicCamera
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement