--// Configuration
local FLY_SPEED = 50
local TOGGLE_KEY = Enum.KeyCode.E
--// Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
--// Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Flying = false
local Control = {F = 0, B = 0, L = 0, R = 0, Q = 0, E = 0}
--// Handle Respawn
LocalPlayer.CharacterAdded:Connect(function(NewCharacter)
Character = NewCharacter
Humanoid = NewCharacter:WaitForChild("Humanoid")
RootPart = NewCharacter:WaitForChild("HumanoidRootPart")
if Flying then ToggleFly() end
end)
--// Flight Logic
local BodyGyro, BodyVelocity
function ToggleFly()
Flying = not Flying
if Flying then
-- Anchor animations/physics slightly so you don't fall
Humanoid.PlatformStand = true
-- Create body movers to handle the flight forces
BodyGyro = Instance.new("BodyGyro")
BodyGyro.P = 9e4
BodyGyro.maxTorque = Vector3.new(9e9, 9e9, 9e9)
BodyGyro.cframe = RootPart.CFrame
BodyGyro.Parent = RootPart
BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.velocity = Vector3.new(0, 0.1, 0)
BodyVelocity.maxForce = Vector3.new(9e9, 9e9, 9e9)
BodyVelocity.Parent = RootPart
-- Flight Loop
task.spawn(function()
while Flying and RootPart and BodyVelocity and BodyGyro do
RunService.RenderStepped:Wait()
-- Calculate movement direction based on camera angle
local Camera = workspace.CurrentCamera
local Direction = Vector3.new()
if Control.F + Control.B ~= 0 or Control.L + Control.R ~= 0 or Control.Q + Control.E ~= 0 then
Direction = Camera.CFrame.LookVector * (Control.F + Control.B) +
Camera.CFrame.RightVector * (Control.L + Control.R) +
Vector3.new(0, Control.Q + Control.E, 0)
end
-- Apply velocity
BodyVelocity.velocity = Direction * FLY_SPEED
BodyGyro.cframe = Camera.CFrame
end
end)
else
-- Clean up forces when turning off
Humanoid.PlatformStand = false
if BodyGyro then BodyGyro:Destroy() end
if BodyVelocity then BodyVelocity:Destroy() end
end
end
--// Controls Listener
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == TOGGLE_KEY then
ToggleFly()
elseif input.KeyCode == Enum.KeyCode.W then Control.F = 1
elseif input.KeyCode == Enum.KeyCode.S then Control.B = -1
elseif input.KeyCode == Enum.KeyCode.A then Control.L = -1
elseif input.KeyCode == Enum.KeyCode.D then Control.R = 1
elseif input.KeyCode == Enum.KeyCode.Space then Control.Q = 1 -- Go Up
elseif input.KeyCode == Enum.KeyCode.LeftShift then Control.E = -1 -- Go Down
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then Control.F = 0
elseif input.KeyCode == Enum.KeyCode.S then Control.B = 0
elseif input.KeyCode == Enum.KeyCode.A then Control.L = 0
elseif input.KeyCode == Enum.KeyCode.D then Control.R = 0
elseif input.KeyCode == Enum.KeyCode.Space then Control.Q = 0
elseif input.KeyCode == Enum.KeyCode.LeftShift then Control.E = 0
end
end)
Comments
No comments yet
Be the first to share your thoughts!