-
Notifications
You must be signed in to change notification settings - Fork 48
Description
-- Lugar: StarterPlayer > StarterCharacterScripts (LocalScript)
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
-- Variables de vuelo
local isFlying = false
local flying = {}
flying.speed = 50 -- Velocidad de vuelo
flying.bodyVelocity = nil
flying.bodyGyro = nil
-- Función para activar el vuelo
local function enableFlying()
if isFlying then return end
isFlying = true
print("
-- Crear BodyVelocity para el movimiento
flying.bodyVelocity = Instance.new("BodyVelocity")
flying.bodyVelocity.Velocity = Vector3.new(0, 0, 0)
flying.bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
flying.bodyVelocity.Parent = rootPart
-- Crear BodyGyro para la rotación
flying.bodyGyro = Instance.new("BodyGyro")
flying.bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
flying.bodyGyro.P = 9000
flying.bodyGyro.Parent = rootPart
flying.bodyGyro.CFrame = rootPart.CFrame
-- Loop de vuelo
local flyConnection
flyConnection = RunService.RenderStepped:Connect(function()
if not isFlying or not rootPart.Parent then
flyConnection:Disconnect()
return
end
-- Obtener la dirección de la cámara
local camera = workspace.CurrentCamera
local moveDirection = Vector3.new(0, 0, 0)
-- Controles de movimiento (WASD o joystick móvil)
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
moveDirection = moveDirection + (camera.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
moveDirection = moveDirection - (camera.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
moveDirection = moveDirection - camera.CFrame.RightVector
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
moveDirection = moveDirection + camera.CFrame.RightVector
end
-- Subir con espacio, bajar con Ctrl
if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
moveDirection = moveDirection + Vector3.new(0, 1, 0)
end
if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
moveDirection = moveDirection - Vector3.new(0, 1, 0)
end
-- Normalizar dirección y aplicar velocidad
if moveDirection.Magnitude > 0 then
moveDirection = moveDirection.Unit
end
flying.bodyVelocity.Velocity = moveDirection * flying.speed
flying.bodyGyro.CFrame = camera.CFrame
end)
end
-- Función para desactivar el vuelo
local function disableFlying()
if not isFlying then return end
isFlying = false
print("❌ ¡VUELO DESACTIVADO!")
-- Eliminar BodyVelocity y BodyGyro
if flying.bodyVelocity then
flying.bodyVelocity:Destroy()
flying.bodyVelocity = nil
end
if flying.bodyGyro then
flying.bodyGyro:Destroy()
flying.bodyGyro = nil
end
end
-- Función para alternar vuelo
local function toggleFlying()
if isFlying then
disableFlying()
else
enableFlying()
end
end
-- Control por teclado (tecla F)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then
toggleFlying()
end
end)
-- Botón virtual para móvil
local function onFlightButton(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
toggleFlying()
end
end
ContextActionService:BindAction("ToggleFlight", onFlightButton, true, Enum.KeyCode.F)
ContextActionService:SetPosition("ToggleFlight", UDim2.new(0.85, 0, 0.5, 0))
-- Limpiar cuando el personaje muere
humanoid.Died:Connect(function()
disableFlying()
end)