Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
- local Char = player.Character
- if not Char or not Char.Parent then
- Char = player.CharacterAdded:wait()
- end
- local UserInputService = game:GetService("UserInputService")
- local serverVars = {
- airacceleration = 10;
- maxacceleration = 60;
- friction = 1
- }
- local accelDir = Vector3.new(0,0,0)
- local playerVelocity = Vector3.new(0,0,0)
- local bodyVelocity = Instance.new("BodyVelocity",Char.HumanoidRootPart)
- -- bodyVelocity.maxForce = Vector3.new() clearly doesn't work
- bodyVelocity.maxForce = Vector3.new(math.huge, 0, math.huge)
- bodyVelocity.velocity = Vector3.new()
- local prevVelocity = Vector3.new(0,0,0)
- local w = UserInputService:IsKeyDown(Enum.KeyCode.W)
- local s = UserInputService:IsKeyDown(Enum.KeyCode.S)
- local a = UserInputService:IsKeyDown(Enum.KeyCode.A)
- local d = UserInputService:IsKeyDown(Enum.KeyCode.D)
- local function onGround()
- local checkRay = Ray.new(Char:GetPrimaryPartCFrame().p, Vector3.new(0,-6,0))
- local hit,pos,normal = workspace:FindPartOnRay(checkRay, Char)
- if hit ~= nil then
- return true
- end
- return false
- end
- local function accelerate(Direction, prevVelocity, DeltaTime)
- local projection = prevVelocity:Dot(Direction.Unit)
- local acceleration = serverVars.airacceleration * DeltaTime
- print(acceleration, projection)
- if (projection + acceleration > serverVars.maxacceleration) then
- acceleration = serverVars.maxacceleration-projection
- elseif (projection + acceleration < -serverVars.maxacceleration) then
- acceleration = serverVars.maxacceleration+projection
- end
- return prevVelocity + Direction * acceleration
- end
- local function groundMove(Direction, prevVelocity, DeltaTime)
- local speed = prevVelocity.magnitude
- if speed ~= 0 and onGround() then
- local drop = speed * 1 * DeltaTime
- prevVelocity = prevVelocity * math.max(speed - drop, 0) / speed
- end
- return accelerate(Direction, prevVelocity, DeltaTime)
- end
- local Time = tick()
- function updateAirStrafe(Direction, prevVelocity, DeltaTime)
- local W, A, S, D = 0,0,0,0 --stores what buttons are being pressed
- if UserInputService:IsKeyDown(Enum.KeyCode.W) then --if pressing W, then store that W is being pressed
- W = 1
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.A) then
- A = 1
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.S) then
- S = 1
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.D) then
- D = 1
- end
- if W > 0 or A > 0 or S > 0 or D > 0 then --if they want to move
- bodyVelocity.maxForce = Vector3.new(math.huge, 0, math.huge) --give bodyVelocity some force
- --figure out what direction they want to move based on keys pressed
- Direction = Vector3.new(W - S, 0, D - A) --maybe A - D
- --get what direction Velocity should be in world space
- playerVelocity = accelerate(Direction, bodyVelocity.Velocity, DeltaTime) -- I think this is what accelerate does, not sure
- bodyVelocity.MaxForce = Vector3.new(0,0,0)
- if not onGround() then
- playerVelocity = accelerate(accelDir, playerVelocity, DeltaTime)
- playerVelocity = playerVelocity + (Vector3.new(0, workspace.Gravity ,0) * DeltaTime)
- prevVelocity = playerVelocity
- local posDelta = bodyVelocity.Velocity + playerVelocity
- print(posDelta)
- end
- end
- end
- spawn(function()
- game:GetService("RunService").RenderStepped:Connect(function(dt)
- accelDir = Vector3.new()
- updateAirStrafe(dt)
- end)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement