Advertisement
unknownexploits

bhopp

Feb 18th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. local mv = {}
  2.  
  3. local player
  4. local character
  5. local collider
  6. local camera
  7. local input
  8. local collider
  9. local playerGrounded
  10. local playerVelocity
  11. local jumping
  12. local moveInputSum
  13. local dt = 1/60
  14. local partYRatio
  15. local partZRatio
  16. local cameraYaw
  17. local cameraLook
  18. local movementPosition
  19. local movementVelocity
  20. local gravityForce
  21.  
  22. local airAccelerate = { }
  23. local airMaxSpeed = { } --only limits projection of velocity onto acceleration
  24. local groundAccelerate = { }
  25. local groundMaxVelocity = { } --only limits projection of velocity onto acceleration
  26. local friction = { }
  27. local playerTorsoToGround = { }
  28. local movementStickDistance = { }
  29. local jumpVelocity = { }
  30. local movementPositionForce = { }
  31. local movementVelocityForce = { }
  32. local maxMovementPitch = { }
  33. local rayYLength = { }
  34. local movementPositionD = { }
  35. local movementPositionP = { }
  36. local movementVelocityP = { }
  37. local gravity = { }
  38.  
  39.  
  40.  
  41. function mv.init(Player, Camera, Input)
  42. player = game.Players.LocalPlayer
  43. character = player.Character
  44. collider = character.Torso
  45. camera = Camera
  46. input = Input
  47. playerVelocity = 0
  48. playerGrounded = false
  49. moveInputSum = {
  50. ["forward"] = 0,
  51. ["side"] = 0 --left is positive
  52. }
  53.  
  54. airAccelerate = 10000
  55. airMaxSpeed = 2.4
  56. groundAccelerate = 250
  57. groundMaxVelocity = 20
  58. friction = 10
  59. playerTorsoToGround = 3
  60. movementStickDistance = 0.5
  61. jumpVelocity = 25
  62. movementPositionForce = 400000
  63. movementVelocityForce = 300000
  64. maxMovementPitch = 0.6
  65. rayYLength = playerTorsoToGround + movementStickDistance
  66. movementPositionD = 125
  67. movementPositionP = 14000
  68. movementVelocityP = 1500
  69. gravity = 0.4
  70.  
  71. end
  72.  
  73. function mv.initBodyMovers()
  74. movementPosition = Instance.new("BodyPosition", collider)
  75. movementPosition.Name = "movementPosition"
  76. movementPosition.D = movementPositionD
  77. movementPosition.P = movementPositionP
  78. movementPosition.maxForce = Vector3.new()
  79. movementPosition.position = Vector3.new()
  80.  
  81. movementVelocity = Instance.new("BodyVelocity", collider)
  82. movementVelocity.Name = "movementVelocity"
  83. movementVelocity.P = movementVelocityP
  84. movementVelocity.maxForce = Vector3.new()
  85. movementVelocity.velocity = Vector3.new()
  86.  
  87. gravityForce = Instance.new("BodyForce", collider)
  88. gravityForce.Name = "gravityForce"
  89. gravityForce.force = Vector3.new(0, (1-gravity)*196.2, 0) * getCharacterMass()
  90. end
  91.  
  92. function mv.update(deltaTime)
  93. dt = deltaTime
  94. updateMoveInputSum()
  95. cameraYaw = getYaw()
  96. cameraLook = cameraYaw.lookVector
  97. if cameraLook == nil then
  98. return
  99. end
  100. local hitPart, hitPosition, hitNormal, yRatio, zRatio = findCollisionRay()
  101. partYRatio = yRatio
  102. partZRatio = zRatio
  103.  
  104. playerGrounded = hitPart ~= nil and true or false
  105. playerVelocity = collider.Velocity - Vector3.new(0, collider.Velocity.y, 0)
  106. if playerGrounded and (input["Space"] or jumping) then
  107. jumping = true
  108. else
  109. jumping = false
  110. end
  111.  
  112. setCharacterRotation()
  113. if jumping then
  114. jump()
  115. print(math.random())
  116. elseif playerGrounded then
  117. run(hitPosition)
  118. else
  119. air()
  120. end
  121.  
  122. end
  123.  
  124. function updateMoveInputSum()
  125. moveInputSum["forward"] = input["W"] == true and 1 or 0
  126. moveInputSum["forward"] = input["S"] == true and moveInputSum["forward"] - 1 or moveInputSum["forward"]
  127. moveInputSum["side"] = input["A"] == true and 1 or 0
  128. moveInputSum["side"] = input["D"] == true and moveInputSum["side"] - 1 or moveInputSum["side"]
  129. end
  130.  
  131. function findCollisionRay()
  132. local torsoCFrame = character.Torso.CFrame
  133. local ignoreList = {character, camera}
  134. local rays = {
  135. Ray.new(character.Torso.Position, Vector3.new(0, -rayYLength, 0)),
  136. Ray.new((torsoCFrame * CFrame.new(-0.8,0,0)).p, Vector3.new(0, -rayYLength, 0)),
  137. Ray.new((torsoCFrame * CFrame.new(0.8,0,0)).p, Vector3.new(0, -rayYLength, 0)),
  138. Ray.new((torsoCFrame * CFrame.new(0,0,0.8)).p, Vector3.new(0, -rayYLength, 0)),
  139. Ray.new((torsoCFrame * CFrame.new(0,0,-0.8)).p, Vector3.new(0, -rayYLength, 0))
  140. }
  141. local rayReturns = {}
  142.  
  143. local i
  144. for i = 1, #rays do
  145. local part, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(rays[i],ignoreList)
  146. if part == nil then
  147. position = Vector3.new(0,-3000000,0)
  148. end
  149. if i == 1 then
  150. table.insert(rayReturns, {part, position, normal})
  151. else
  152. local yPos = position.y
  153. if yPos <= rayReturns[#rayReturns][2].y then
  154. table.insert(rayReturns, {part, position, normal})
  155. else
  156. local j
  157. for j = 1, #rayReturns do
  158. if yPos >= rayReturns[j][2].y then
  159. table.insert(rayReturns, j, {part, position, normal})
  160. end
  161. end
  162. end
  163. end
  164. end
  165.  
  166. i = 1
  167. local yRatio, zRatio = getPartYRatio(rayReturns[i][3])
  168. while magnitude2D(yRatio, zRatio) > maxMovementPitch and i<#rayReturns do
  169. i = i + 1
  170. if rayReturns[i][1] then
  171. yRatio, zRatio = getPartYRatio(rayReturns[i][3])
  172. end
  173. end
  174.  
  175. return rayReturns[i][1], rayReturns[i][2], rayReturns[i][3], yRatio, zRatio
  176. end
  177.  
  178. function setCharacterRotation()
  179. local rotationLook = collider.Position + camera.CoordinateFrame.lookVector
  180. collider.CFrame = CFrame.new(collider.Position, Vector3.new(rotationLook.x, collider.Position.y, rotationLook.z))
  181. collider.RotVelocity = Vector3.new()
  182. end
  183.  
  184. function jump()
  185. collider.Velocity = Vector3.new(collider.Velocity.x, jumpVelocity, collider.Velocity.z)
  186. air()
  187. end
  188.  
  189. function air()
  190. movementPosition.maxForce = Vector3.new()
  191. movementVelocity.velocity = getMovementVelocity(collider.Velocity, airAccelerate, airMaxSpeed)
  192. movementVelocity.maxForce = getMovementVelocityAirForce()
  193. end
  194.  
  195. function run(hitPosition)
  196. local playerSpeed = collider.Velocity.magnitude
  197. local mVelocity = collider.Velocity
  198.  
  199. if playerSpeed ~= 0 then
  200. local drop = playerSpeed * friction * dt;
  201. mVelocity = mVelocity * math.max(playerSpeed - drop, 0) / playerSpeed;
  202. end
  203.  
  204. movementPosition.position = hitPosition + Vector3.new(0,playerTorsoToGround,0)
  205. movementPosition.maxForce = Vector3.new(0,movementPositionForce,0)
  206. movementVelocity.velocity = getMovementVelocity(mVelocity, groundAccelerate, groundMaxVelocity)
  207. local VelocityForce = getMovementVelocityForce()
  208. movementVelocity.maxForce = VelocityForce
  209. movementVelocity.P = movementVelocityP
  210. end
  211.  
  212. function getMovementVelocity(prevVelocity, accelerate, maxVelocity)
  213. local accelForward = cameraLook * moveInputSum["forward"]
  214. local accelSide = (cameraYaw * CFrame.Angles(0,math.rad(90),0)).lookVector * moveInputSum["side"];
  215. local accelDir = (accelForward+accelSide).unit;
  216. if moveInputSum["forward"] == 0 and moveInputSum["side"] == 0 then --avoids divide 0 errors
  217. accelDir = Vector3.new(0,0,0);
  218. end
  219.  
  220. local projVel = prevVelocity:Dot(accelDir);
  221. local accelVel = accelerate * dt;
  222.  
  223. if (projVel + accelVel > maxVelocity) then
  224. accelVel = math.max(maxVelocity - projVel, 0);
  225. end
  226.  
  227. return prevVelocity + accelDir * accelVel;
  228. end
  229.  
  230. function getMovementVelocityForce()
  231.  
  232. return Vector3.new(movementVelocityForce,0,movementVelocityForce)
  233. end
  234.  
  235. function getMovementVelocityAirForce()
  236. local accelForward = cameraLook * moveInputSum["forward"];
  237. local accelSide = (cameraYaw * CFrame.Angles(0,math.rad(90),0)).lookVector * moveInputSum["side"]
  238. local accelDir = (accelForward+accelSide).unit
  239. if moveInputSum["forward"] == 0 and moveInputSum["side"] == 0 then
  240. accelDir = Vector3.new(0,0,0);
  241. end
  242.  
  243. local xp = math.abs(accelDir.x)
  244. local zp = math.abs(accelDir.z)
  245.  
  246. return Vector3.new(movementVelocityForce*xp,0,movementVelocityForce*zp)
  247. end
  248.  
  249. function getPartYRatio(normal)
  250. local partYawVector = Vector3.new(-normal.x, 0, -normal.z)
  251. if partYawVector.magnitude == 0 then
  252. return 0,0
  253. else
  254. local partPitch = math.atan2(partYawVector.magnitude,normal.y)/(math.pi/2)
  255. local vector = Vector3.new(cameraLook.x, 0, cameraLook.z)*partPitch
  256. return vector:Dot(partYawVector), -partYawVector:Cross(vector).y
  257. end
  258. end
  259.  
  260. function getYaw() --returns CFrame
  261. return camera.CoordinateFrame*CFrame.Angles(-getPitch(),0,0)
  262. end
  263.  
  264. function getPitch() --returns number
  265. return math.pi/2 - math.acos(camera.CoordinateFrame.lookVector:Dot(Vector3.new(0,1,0)))
  266. end
  267.  
  268. function getCharacterMass()
  269. return character.Torso:GetMass() + character.Head:GetMass()
  270. end
  271.  
  272. function magnitude2D(x,z)
  273. return math.sqrt(x*x+z*z)
  274. end
  275.  
  276.  
  277.  
  278.  
  279. return mv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement