local player = game.Players.
LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera
local flying = false
local flySpeed = 50 -- Adjust fly speed here
local bodyVelocity
local bodyGyro
-- Function to start flying
local function startFlying()
if not flying then
flying = true
humanoid.WalkSpeed = 0 -- Disable walking
humanoid.PlatformStand = true -- Smooth flying
bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, 0, 0)
bodyVelocity.MaxForce = Vector3.new(40000, 40000, 40000)
bodyVelocity.Parent = rootPart
bodyGyro = Instance.new("BodyGyro")
bodyGyro.P = 3000
bodyGyro.MaxTorque = Vector3.new(40000, 40000, 40000)
bodyGyro.Parent = rootPart
end
end
-- Function to stop flying
local function stopFlying()
if flying then
flying = false
humanoid.WalkSpeed = 16 -- Restore default walk speed
humanoid.PlatformStand = false
if bodyVelocity then bodyVelocity:Destroy() end
if bodyGyro then bodyGyro:Destroy() end
end
end
-- Toggle flying with "F" key
game:GetService("UserInputService").InputBegan:Connect(function(input,
gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.F then
if flying then
stopFlying()
else
startFlying()
end
end
end)
-- Control movement with W, A, S, D
game:GetService("RunService").RenderStepped:Connect(function()
if flying then
local moveDirection = Vector3.new()
local camLook = camera.CFrame.LookVector
local camRight = camera.CFrame.RightVector
-- WASD movement
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.W) then
moveDirection = moveDirection + camLook
end
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.S) then
moveDirection = moveDirection - camLook
end
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.A) then
moveDirection = moveDirection - camRight
end
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.D) then
moveDirection = moveDirection + camRight
end
-- Apply velocity or hover
if moveDirection.Magnitude > 0 then
bodyVelocity.Velocity = moveDirection.Unit * flySpeed
else
bodyVelocity.Velocity = Vector3.new(0, 0, 0) -- Hover in place
end
-- Align with camera
bodyGyro.CFrame = camera.CFrame
end
end)