0% found this document useful (0 votes)
46 views2 pages

Message

This script enables a flying mechanic for a player character in a game, allowing the player to toggle flying on and off using the 'F' key. When flying, the player's walking speed is disabled, and the character can move in the air using the WASD keys while aligned with the camera's direction. The script manages the physics for flying by utilizing BodyVelocity and BodyGyro to control movement and orientation in the air.

Uploaded by

galxayplays77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Message

This script enables a flying mechanic for a player character in a game, allowing the player to toggle flying on and off using the 'F' key. When flying, the player's walking speed is disabled, and the character can move in the air using the WASD keys while aligned with the camera's direction. The script manages the physics for flying by utilizing BodyVelocity and BodyGyro to control movement and orientation in the air.

Uploaded by

galxayplays77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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)

You might also like