Advertisement
Guest User

im probably stupid

a guest
Dec 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
  3. local Char = player.Character
  4.  
  5. if not Char or not Char.Parent then
  6. Char = player.CharacterAdded:wait()
  7. end
  8.  
  9. local UserInputService = game:GetService("UserInputService")
  10.  
  11. local serverVars = {
  12. airacceleration = 10;
  13. maxacceleration = 60;
  14. friction = 1
  15. }
  16.  
  17. local accelDir = Vector3.new(0,0,0)
  18. local playerVelocity = Vector3.new(0,0,0)
  19. local bodyVelocity = Instance.new("BodyVelocity",Char.HumanoidRootPart)
  20. -- bodyVelocity.maxForce = Vector3.new() clearly doesn't work
  21. bodyVelocity.maxForce = Vector3.new(math.huge, 0, math.huge)
  22. bodyVelocity.velocity = Vector3.new()
  23. local prevVelocity = Vector3.new(0,0,0)
  24.  
  25. local w = UserInputService:IsKeyDown(Enum.KeyCode.W)
  26. local s = UserInputService:IsKeyDown(Enum.KeyCode.S)
  27. local a = UserInputService:IsKeyDown(Enum.KeyCode.A)
  28. local d = UserInputService:IsKeyDown(Enum.KeyCode.D)
  29.  
  30.  
  31. local function onGround()
  32. local checkRay = Ray.new(Char:GetPrimaryPartCFrame().p, Vector3.new(0,-6,0))
  33. local hit,pos,normal = workspace:FindPartOnRay(checkRay, Char)
  34. if hit ~= nil then
  35. return true
  36. end
  37. return false
  38. end
  39.  
  40. local function accelerate(Direction, prevVelocity, DeltaTime)
  41. local projection = prevVelocity:Dot(Direction.Unit)
  42. local acceleration = serverVars.airacceleration * DeltaTime
  43. print(acceleration, projection)
  44. if (projection + acceleration > serverVars.maxacceleration) then
  45. acceleration = serverVars.maxacceleration-projection
  46. elseif (projection + acceleration < -serverVars.maxacceleration) then
  47. acceleration = serverVars.maxacceleration+projection
  48. end
  49. return prevVelocity + Direction * acceleration
  50. end
  51.  
  52. local function groundMove(Direction, prevVelocity, DeltaTime)
  53. local speed = prevVelocity.magnitude
  54. if speed ~= 0 and onGround() then
  55. local drop = speed * 1 * DeltaTime
  56. prevVelocity = prevVelocity * math.max(speed - drop, 0) / speed
  57. end
  58. return accelerate(Direction, prevVelocity, DeltaTime)
  59. end
  60.  
  61.  
  62. local Time = tick()
  63. function updateAirStrafe(Direction, prevVelocity, DeltaTime)
  64.  
  65. local W, A, S, D = 0,0,0,0 --stores what buttons are being pressed
  66. if UserInputService:IsKeyDown(Enum.KeyCode.W) then --if pressing W, then store that W is being pressed
  67. W = 1
  68. end
  69. if UserInputService:IsKeyDown(Enum.KeyCode.A) then
  70. A = 1
  71. end
  72. if UserInputService:IsKeyDown(Enum.KeyCode.S) then
  73. S = 1
  74. end
  75. if UserInputService:IsKeyDown(Enum.KeyCode.D) then
  76. D = 1
  77. end
  78.  
  79. if W > 0 or A > 0 or S > 0 or D > 0 then --if they want to move
  80. bodyVelocity.maxForce = Vector3.new(math.huge, 0, math.huge) --give bodyVelocity some force
  81.  
  82. --figure out what direction they want to move based on keys pressed
  83. Direction = Vector3.new(W - S, 0, D - A) --maybe A - D
  84.  
  85. --get what direction Velocity should be in world space
  86. playerVelocity = accelerate(Direction, bodyVelocity.Velocity, DeltaTime) -- I think this is what accelerate does, not sure
  87. bodyVelocity.MaxForce = Vector3.new(0,0,0)
  88.  
  89. if not onGround() then
  90. playerVelocity = accelerate(accelDir, playerVelocity, DeltaTime)
  91. playerVelocity = playerVelocity + (Vector3.new(0, workspace.Gravity ,0) * DeltaTime)
  92. prevVelocity = playerVelocity
  93. local posDelta = bodyVelocity.Velocity + playerVelocity
  94. print(posDelta)
  95. end
  96. end
  97. end
  98.  
  99. spawn(function()
  100. game:GetService("RunService").RenderStepped:Connect(function(dt)
  101. accelDir = Vector3.new()
  102. updateAirStrafe(dt)
  103. end)
  104. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement