Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local UserInputService = game:GetService("UserInputService")
- local LocalPlayer = game.Players.LocalPlayer
- local flying = false
- local flightSpeed = 50
- local bodyVelocity
- local bodyGyro
- local doublePressTime = 0.5
- local lastPressTime = 0
- -- Change character state to Physics (stops falling animation)
- local function setIdleState()
- local humanoid = LocalPlayer.Character and LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid")
- if humanoid and humanoid:GetState() ~= Enum.HumanoidStateType.Seated then
- humanoid:ChangeState(Enum.HumanoidStateType.Physics)
- end
- end
- -- Start flying
- local function startFlying()
- if flying then return end
- flying = true
- local character = LocalPlayer.Character
- local rootPart = character and character:FindFirstChild("HumanoidRootPart")
- if not rootPart then return end
- bodyVelocity = Instance.new("BodyVelocity")
- bodyVelocity.MaxForce = Vector3.new(1e5, 1e5, 1e5)
- bodyVelocity.Velocity = Vector3.new(0, 0, 0)
- bodyVelocity.Parent = rootPart
- bodyGyro = Instance.new("BodyGyro")
- bodyGyro.MaxTorque = Vector3.new(1e5, 1e5, 1e5)
- bodyGyro.P = 1e4
- bodyGyro.CFrame = rootPart.CFrame
- bodyGyro.Parent = rootPart
- while flying and character.Parent do
- setIdleState()
- local moveDirection = Vector3.new()
- if UserInputService:IsKeyDown(Enum.KeyCode.W) then
- moveDirection += workspace.CurrentCamera.CFrame.LookVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.S) then
- moveDirection -= workspace.CurrentCamera.CFrame.LookVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.A) then
- moveDirection -= workspace.CurrentCamera.CFrame.RightVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.D) then
- moveDirection += workspace.CurrentCamera.CFrame.RightVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
- moveDirection += Vector3.new(0, 1, 0)
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
- moveDirection -= Vector3.new(0, 1, 0)
- end
- if moveDirection.Magnitude > 0 then
- moveDirection = moveDirection.Unit
- bodyVelocity.Velocity = moveDirection * flightSpeed
- else
- bodyVelocity.Velocity = Vector3.zero
- end
- bodyGyro.CFrame = workspace.CurrentCamera.CFrame
- task.wait()
- end
- end
- -- Stop flying
- local function stopFlying()
- flying = false
- if bodyVelocity then bodyVelocity:Destroy() end
- if bodyGyro then bodyGyro:Destroy() end
- end
- -- Double space to toggle flight
- UserInputService.InputBegan:Connect(function(input, processed)
- if processed then return end
- if input.KeyCode == Enum.KeyCode.Space then
- local now = tick()
- if now - lastPressTime < doublePressTime then
- if flying then
- stopFlying()
- else
- startFlying()
- end
- end
- lastPressTime = now
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement