Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local player = game.Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- local humanoid = character:WaitForChild("Humanoid")
- local userInputService = game:GetService("UserInputService")
- local runService = game:GetService("RunService")
- local camera = game.Workspace.CurrentCamera
- local flying = false
- local baseSpeed = 150
- local maxSpeed = 250
- local acceleration = 5
- local currentSpeed = 0
- local lastDirection = nil
- local deceleration = 10
- local bodyVelocity
- local function toggleFly()
- flying = not flying
- if flying then
- bodyVelocity = Instance.new("BodyVelocity")
- bodyVelocity.MaxForce = Vector3.new(1e6, 1e6, 1e6)
- bodyVelocity.P = 1250
- bodyVelocity.Velocity = Vector3.zero
- bodyVelocity.Parent = humanoidRootPart
- currentSpeed = baseSpeed
- else
- if bodyVelocity then
- bodyVelocity:Destroy()
- bodyVelocity = nil
- end
- humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
- lastDirection = nil
- end
- end
- userInputService.InputBegan:Connect(function(input, gameProcessed)
- if gameProcessed then return end
- if input.KeyCode == Enum.KeyCode.X then
- toggleFly()
- end
- end)
- runService.RenderStepped:Connect(function(deltaTime)
- if flying and bodyVelocity then
- local moveDirection = Vector3.new(0, 0, 0)
- local currentDirection = nil
- if userInputService:IsKeyDown(Enum.KeyCode.W) then
- moveDirection = moveDirection + camera.CFrame.LookVector
- currentDirection = "W"
- if lastDirection == "S" then
- currentSpeed = math.max(currentSpeed - deceleration * deltaTime, baseSpeed)
- end
- end
- if userInputService:IsKeyDown(Enum.KeyCode.S) then
- moveDirection = moveDirection - camera.CFrame.LookVector
- currentDirection = "S"
- if lastDirection == "W" then
- currentSpeed = math.max(currentSpeed - deceleration * deltaTime, baseSpeed)
- end
- end
- if userInputService:IsKeyDown(Enum.KeyCode.A) then
- moveDirection = moveDirection - camera.CFrame.RightVector
- currentDirection = "A"
- end
- if userInputService:IsKeyDown(Enum.KeyCode.D) then
- moveDirection = moveDirection + camera.CFrame.RightVector
- currentDirection = "D"
- end
- if moveDirection.Magnitude > 0 then
- if currentDirection ~= lastDirection and not ((currentDirection == "W" and lastDirection == "S") or (currentDirection == "S" and lastDirection == "W")) then
- currentSpeed = baseSpeed
- end
- currentSpeed = math.min(currentSpeed + acceleration * deltaTime, maxSpeed)
- bodyVelocity.Velocity = moveDirection.Unit * currentSpeed
- lastDirection = currentDirection
- else
- bodyVelocity.Velocity = Vector3.zero
- end
- local cameraLookVector = camera.CFrame.LookVector
- humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, humanoidRootPart.Position + cameraLookVector)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement