KrYn0MoRe

csgo bhop

Apr 23rd, 2020 (edited)
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.64 KB | None | 0 0
  1. local ls = [[
  2. local player
  3. local character
  4. local collider
  5. local camera
  6. local input
  7. local collider
  8. local playerGrounded
  9. local playerVelocity
  10. local jumping
  11. local moveInputSum
  12. local dt = 1/60
  13. local partYRatio
  14. local partZRatio
  15. local cameraYaw
  16. local cameraLook
  17. local movementPosition
  18. local movementVelocity
  19. local gravityForce
  20.  
  21. local airAccelerate
  22. local airMaxSpeed
  23. local groundAccelerate
  24. local groundMaxVelocity
  25. local friction
  26. local playerTorsoToGround
  27. local movementStickDistance
  28. local jumpVelocity
  29. local movementPositionForce
  30. local movementVelocityForce
  31. local maxMovementPitch
  32. local rayYLength
  33. local movementPositionD
  34. local movementPositionP
  35. local movementVelocityP
  36. local gravity
  37.  
  38. function init(Player, Camera, Input)
  39.     player = Player
  40.     character = player.Character
  41.     collider = character.HumanoidRootPart
  42.     camera = Camera
  43.     input = Input
  44.     playerVelocity = 0
  45.     playerGrounded = false
  46.     moveInputSum = {
  47.     ["forward"] = 0,
  48.     ["side"]    = 0 --left is positive
  49.     }
  50.    
  51.     airAccelerate           = 20000
  52.     airMaxSpeed             = 2.4
  53.     groundAccelerate        = 400
  54.     groundMaxVelocity       = 500
  55.     friction                = 10
  56.     playerTorsoToGround     = 3
  57.     movementStickDistance   = 0.5
  58.     jumpVelocity            = 52.5
  59.     movementPositionForce   = 500000
  60.     movementVelocityForce   = 300000
  61.     maxMovementPitch        = 1.6
  62.     rayYLength              = playerTorsoToGround + movementStickDistance
  63.     movementPositionD       = 150
  64.     movementPositionP       = 2000
  65.     movementVelocityP       = 3000
  66.     gravity                 = 0.4
  67.    
  68. end
  69.  
  70. function initBodyMovers()
  71.     movementPosition = Instance.new("BodyPosition", collider)
  72.     movementPosition.Name = "movementPosition"
  73.     movementPosition.D = movementPositionD
  74.     movementPosition.P = movementPositionP
  75.     movementPosition.maxForce = Vector3.new()
  76.     movementPosition.position = Vector3.new()
  77.    
  78.     movementVelocity = Instance.new("BodyVelocity", collider)
  79.     movementVelocity.Name = "movementVelocity"
  80.     movementVelocity.P = movementVelocityP
  81.     movementVelocity.maxForce = Vector3.new()
  82.     movementVelocity.velocity = Vector3.new()
  83.    
  84.     gravityForce = Instance.new("BodyForce", collider)
  85.     gravityForce.Name = "gravityForce"
  86.     gravityForce.force = Vector3.new(0, (1-gravity)*196.2, 0) * getCharacterMass()
  87. end
  88.  
  89. function update(deltaTime)
  90.     dt = deltaTime
  91.     updateMoveInputSum()
  92.     cameraYaw = getYaw()
  93.     cameraLook = cameraYaw.lookVector  
  94.     if cameraLook == nil then
  95.         return
  96.     end
  97.     local hitPart, hitPosition, hitNormal, yRatio, zRatio = findCollisionRay()
  98.     partYRatio = yRatio
  99.     partZRatio = zRatio
  100.    
  101.     playerGrounded = hitPart ~= nil and true or false
  102.     playerVelocity = collider.Velocity - Vector3.new(0, collider.Velocity.y, 0)
  103.     if playerGrounded and (input["Space"] or jumping) then
  104.         jumping = true
  105.     else
  106.         jumping = false
  107.     end
  108.    
  109.     setCharacterRotation()
  110.     if jumping then
  111.         jump()
  112.     elseif playerGrounded then
  113.         run(hitPosition)
  114.     else
  115.         air()      
  116.     end
  117.    
  118. end
  119.  
  120. function updateMoveInputSum()
  121.     moveInputSum["forward"] = input["W"] == true and 1 or 0
  122.     moveInputSum["forward"] = input["S"] == true and moveInputSum["forward"] - 1 or moveInputSum["forward"]
  123.     moveInputSum["side"] = input["A"] == true and 1 or 0
  124.     moveInputSum["side"] = input["D"] == true and moveInputSum["side"] - 1 or moveInputSum["side"]
  125. end
  126.  
  127. function findCollisionRay()
  128.     local torsoCFrame = character.HumanoidRootPart.CFrame
  129.     local ignoreList = {character, camera}
  130.     local rays = {
  131.         Ray.new(character.HumanoidRootPart.Position, Vector3.new(0, -rayYLength, 0)),
  132.         Ray.new((torsoCFrame * CFrame.new(-0.8,0,0)).p, Vector3.new(0, -rayYLength, 0)),
  133.         Ray.new((torsoCFrame * CFrame.new(0.8,0,0)).p, Vector3.new(0, -rayYLength, 0)),
  134.         Ray.new((torsoCFrame * CFrame.new(0,0,0.8)).p, Vector3.new(0, -rayYLength, 0)),
  135.         Ray.new((torsoCFrame * CFrame.new(0,0,-0.8)).p, Vector3.new(0, -rayYLength, 0))
  136.     }
  137.     local rayReturns  = {}
  138.    
  139.     local i
  140.     for i = 1, #rays do
  141.         local part, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(rays[i],ignoreList)
  142.         if part == nil then
  143.             position = Vector3.new(0,-3000000,0)
  144.         end
  145.         if i == 1 then
  146.             table.insert(rayReturns, {part, position, normal})
  147.         else
  148.             local yPos = position.y
  149.             if yPos <= rayReturns[#rayReturns][2].y then
  150.                 table.insert(rayReturns, {part, position, normal})
  151.             else
  152.                 local j
  153.                 for j = 1, #rayReturns do
  154.                     if yPos >= rayReturns[j][2].y then
  155.                         table.insert(rayReturns, j, {part, position, normal})
  156.                     end
  157.                 end
  158.             end
  159.         end
  160.     end
  161.    
  162.     i = 1
  163.     local yRatio, zRatio = getPartYRatio(rayReturns[i][3])
  164.     while magnitude2D(yRatio, zRatio) > maxMovementPitch and i<#rayReturns do
  165.         i = i + 1
  166.         if rayReturns[i][1] then
  167.             yRatio, zRatio = getPartYRatio(rayReturns[i][3])
  168.         end
  169.     end
  170.    
  171.     return rayReturns[i][1], rayReturns[i][2], rayReturns[i][3], yRatio, zRatio
  172. end
  173.  
  174. function setCharacterRotation()
  175.     local rotationLook = collider.Position + camera.CoordinateFrame.lookVector
  176.     collider.CFrame = CFrame.new(collider.Position, Vector3.new(rotationLook.x, collider.Position.y, rotationLook.z))
  177.     collider.RotVelocity = Vector3.new()
  178. end
  179.  
  180. function jump()
  181.     collider.Velocity = Vector3.new(collider.Velocity.x, jumpVelocity, collider.Velocity.z)
  182.     air()
  183. end
  184.  
  185. function air()
  186.     movementPosition.maxForce = Vector3.new()
  187.     movementVelocity.velocity = getMovementVelocity(collider.Velocity, airAccelerate, airMaxSpeed)
  188.     movementVelocity.maxForce = getMovementVelocityAirForce()
  189. end
  190.  
  191. function run(hitPosition)
  192.     local playerSpeed = collider.Velocity.magnitude
  193.     local mVelocity = collider.Velocity
  194.    
  195.     if playerSpeed ~= 0 then
  196.         local drop = playerSpeed * friction * dt;
  197.         mVelocity = mVelocity * math.max(playerSpeed - drop, 0) / playerSpeed;
  198.     end
  199.    
  200.     movementPosition.position = hitPosition + Vector3.new(0,playerTorsoToGround,0)
  201.     movementPosition.maxForce = Vector3.new(0,movementPositionForce,0)
  202.     movementVelocity.velocity = getMovementVelocity(mVelocity, groundAccelerate, groundMaxVelocity)
  203.     local VelocityForce = getMovementVelocityForce()
  204.     movementVelocity.maxForce = VelocityForce
  205.     movementVelocity.P = movementVelocityP
  206. end
  207.  
  208. function getMovementVelocity(prevVelocity, accelerate, maxVelocity)
  209.     local accelForward = cameraLook * moveInputSum["forward"]
  210.     local accelSide = (cameraYaw * CFrame.Angles(0,math.rad(90),0)).lookVector * moveInputSum["side"];
  211.     local accelDir = (accelForward+accelSide).unit;
  212.     if moveInputSum["forward"] == 0 and moveInputSum["side"] == 0 then --avoids divide 0 errors
  213.         accelDir = Vector3.new(0,0,0);
  214.     end
  215.    
  216.     local projVel =  prevVelocity:Dot(accelDir);
  217.     local accelVel = accelerate * dt;
  218.    
  219.     if (projVel + accelVel > maxVelocity) then
  220.         accelVel = math.max(maxVelocity - projVel, 0);
  221.     end
  222.    
  223.     return prevVelocity + accelDir * accelVel;
  224. end
  225.  
  226. function getMovementVelocityForce()
  227.  
  228.     return Vector3.new(movementVelocityForce,0,movementVelocityForce)
  229. end
  230.  
  231. function getMovementVelocityAirForce()
  232.     local accelForward = cameraLook * moveInputSum["forward"];
  233.     local accelSide = (cameraYaw * CFrame.Angles(0,math.rad(90),0)).lookVector * moveInputSum["side"]
  234.     local accelDir = (accelForward+accelSide).unit
  235.     if moveInputSum["forward"] == 0 and moveInputSum["side"] == 0 then
  236.         accelDir = Vector3.new(0,0,0);
  237.     end
  238.    
  239.     local xp = math.abs(accelDir.x)
  240.     local zp = math.abs(accelDir.z)
  241.    
  242.     return Vector3.new(movementVelocityForce*xp,0,movementVelocityForce*zp)
  243. end
  244.  
  245. function getPartYRatio(normal)
  246.     local partYawVector = Vector3.new(-normal.x, 0, -normal.z)
  247.     if partYawVector.magnitude == 0 then
  248.         return 0,0
  249.     else
  250.         local partPitch = math.atan2(partYawVector.magnitude,normal.y)/(math.pi/2)
  251.         local vector = Vector3.new(cameraLook.x, 0, cameraLook.z)*partPitch
  252.         return vector:Dot(partYawVector), -partYawVector:Cross(vector).y
  253.     end
  254. end
  255.  
  256. function getYaw() --returns CFrame
  257.     return camera.CoordinateFrame*CFrame.Angles(-getPitch(),0,0)
  258. end
  259.  
  260. function getPitch() --returns number
  261.     return math.pi/2 - math.acos(camera.CoordinateFrame.lookVector:Dot(Vector3.new(0,1,0)))
  262. end
  263.  
  264. function getCharacterMass()
  265.     return character.HumanoidRootPart:GetMass() + character.Head:GetMass()
  266. end
  267.  
  268.  
  269. function sinit(player, position)
  270.     local gui = Instance.new("ScreenGui", script)
  271.     display = Instance.new("TextLabel", gui)
  272.     display.Name = "SpeedDisplay"
  273.     display.Size = UDim2.new(0,100,0,30)
  274.     display.Position = position
  275.     display.BackgroundTransparency = 1
  276.     display.FontSize = "Size18"
  277.     display.TextStrokeTransparency = 0
  278.     display.TextStrokeColor3 = Color3.new(0,0,0)
  279.     display.TextColor3 = Color3.new(1,1,1)
  280.     display.TextWrapped = false
  281. end
  282.  
  283. function magnitude2D(x,z)
  284.     return math.sqrt(x*x+z*z)
  285. end
  286. local inputKeys = {
  287.     ["W"] = false,
  288.     ["S"] = false,
  289.     ["A"] = false,
  290.     ["D"] = false,
  291.     ["Space"] = false,
  292.     ["LMB"] = false,
  293.     ["RMB"] = false
  294. }
  295. local plr = game:GetService("Players").LocalPlayer
  296. script.Parent = plr.PlayerGui
  297. local camera = workspace.CurrentCamera
  298. local UserInputService = game:GetService("UserInputService")
  299. function onInput(input, gameProcessedEvent)
  300.     local inputState
  301.     --print(input.KeyCode)
  302.     if input.UserInputState == Enum.UserInputState.Begin then
  303.         inputState = true
  304.     elseif input.UserInputState == Enum.UserInputState.End then
  305.         inputState = false
  306.     else
  307.         return
  308.     end
  309.    
  310.     if input.UserInputType == Enum.UserInputType.Keyboard then
  311.         local key = input.KeyCode.Name
  312.         if inputKeys[key] ~= nil then
  313.             inputKeys[key] = inputState
  314.         end
  315.     elseif input.UserInputType == Enum.UserInputType.MouseButton1 then --LMB down
  316.         inputKeys.LMB = inputState
  317.     elseif input.UserInputType == Enum.UserInputType.MouseButton2 then --RMB down
  318.         inputKeys.RMB = inputState
  319.     end
  320. end
  321.  
  322. local prevUpdateTime = nil
  323. local updateDT = 1/60
  324.  
  325. function setDeltaTime() --seconds
  326.     local UpdateTime = tick()
  327.     if prevUpdateTime ~= nil then
  328.         updateDT = (UpdateTime - prevUpdateTime)
  329.     else
  330.         updateDT = 1/60
  331.     end
  332.     prevUpdateTime = UpdateTime
  333. end
  334. function supdate(vector3)
  335.     vector3 = vector3 - Vector3.new(0,vector3.y,0)
  336.     local speed = vector3.magnitude
  337.     display.Text = string.format("HSpeed: %.2f", speed)
  338. end
  339. function updateLoop()
  340.     setDeltaTime();
  341.     update(updateDT);
  342.     supdate(player.Character.HumanoidRootPart.Velocity);
  343. end
  344. function main()
  345.     local a = plr.Character:FindFirstChildOfClass("Humanoid") or plr.Character:WaitForChild("Humanoid");
  346.     a.PlatformStand = true
  347.     --init movement
  348.     init(plr, camera, inputKeys);
  349.     initBodyMovers();
  350.        
  351.     --connect input
  352.     sinit(plr, UDim2.new(0.9, -100, 0.5, 50))
  353.     UserInputService.InputBegan:connect(onInput);
  354.     UserInputService.InputEnded:connect(onInput);
  355.     --connect updateloop
  356.     game:GetService("RunService"):BindToRenderStep("updateLoop", 1, updateLoop);
  357.    
  358.     --rip
  359. end
  360. main()
  361. ]]
  362.  
  363. local plr = owner
  364. local char = plr.Character
  365.  
  366. NLS(ls,char)
  367.  
  368. local theme = Instance.new("Sound")
  369. theme.SoundId = 'rbxassetid://2609146554'
  370. theme.Volume = 1
  371. theme.Looped = true
  372. theme.Parent = char['HumanoidRootPart']
  373. theme:Play()
Add Comment
Please, Sign In to add comment