Advertisement
JannickP8

Simple Fly

Jan 19th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. local player = game.Players.LocalPlayer
  2.     local character = player.Character or player.CharacterAdded:Wait()
  3.     local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  4.     local humanoid = character:WaitForChild("Humanoid")
  5.     local userInputService = game:GetService("UserInputService")
  6.     local runService = game:GetService("RunService")
  7.     local camera = game.Workspace.CurrentCamera
  8.  
  9.     local flying = false
  10.     local baseSpeed = 150
  11.     local maxSpeed = 250
  12.     local acceleration = 5
  13.     local currentSpeed = 0
  14.     local lastDirection = nil
  15.     local deceleration = 10
  16.     local bodyVelocity
  17.  
  18.    
  19.     local function toggleFly()
  20.         flying = not flying
  21.         if flying then
  22.            
  23.             bodyVelocity = Instance.new("BodyVelocity")
  24.             bodyVelocity.MaxForce = Vector3.new(1e6, 1e6, 1e6)
  25.             bodyVelocity.P = 1250
  26.             bodyVelocity.Velocity = Vector3.zero
  27.             bodyVelocity.Parent = humanoidRootPart
  28.             currentSpeed = baseSpeed
  29.         else
  30.              
  31.             if bodyVelocity then
  32.                 bodyVelocity:Destroy()
  33.                 bodyVelocity = nil
  34.             end
  35.             humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
  36.             lastDirection = nil
  37.         end
  38.     end
  39.  
  40.    
  41.     userInputService.InputBegan:Connect(function(input, gameProcessed)
  42.         if gameProcessed then return end
  43.         if input.KeyCode == Enum.KeyCode.X then
  44.             toggleFly()
  45.         end
  46.     end)
  47.  
  48.    
  49.     runService.RenderStepped:Connect(function(deltaTime)
  50.         if flying and bodyVelocity then
  51.             local moveDirection = Vector3.new(0, 0, 0)
  52.             local currentDirection = nil
  53.  
  54.             if userInputService:IsKeyDown(Enum.KeyCode.W) then
  55.                 moveDirection = moveDirection + camera.CFrame.LookVector
  56.                 currentDirection = "W"
  57.                 if lastDirection == "S" then
  58.                     currentSpeed = math.max(currentSpeed - deceleration * deltaTime, baseSpeed)
  59.                 end
  60.             end
  61.             if userInputService:IsKeyDown(Enum.KeyCode.S) then
  62.                 moveDirection = moveDirection - camera.CFrame.LookVector
  63.                 currentDirection = "S"
  64.                 if lastDirection == "W" then
  65.                     currentSpeed = math.max(currentSpeed - deceleration * deltaTime, baseSpeed)
  66.                 end
  67.             end
  68.             if userInputService:IsKeyDown(Enum.KeyCode.A) then
  69.                 moveDirection = moveDirection - camera.CFrame.RightVector
  70.                 currentDirection = "A"
  71.             end
  72.             if userInputService:IsKeyDown(Enum.KeyCode.D) then
  73.                 moveDirection = moveDirection + camera.CFrame.RightVector
  74.                 currentDirection = "D"
  75.             end
  76.  
  77.             if moveDirection.Magnitude > 0 then
  78.                 if currentDirection ~= lastDirection and not ((currentDirection == "W" and lastDirection == "S") or (currentDirection == "S" and lastDirection == "W")) then
  79.                     currentSpeed = baseSpeed
  80.                 end
  81.                 currentSpeed = math.min(currentSpeed + acceleration * deltaTime, maxSpeed)
  82.                 bodyVelocity.Velocity = moveDirection.Unit * currentSpeed
  83.                 lastDirection = currentDirection
  84.             else
  85.                 bodyVelocity.Velocity = Vector3.zero
  86.             end
  87.  
  88.            
  89.             local cameraLookVector = camera.CFrame.LookVector
  90.             humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, humanoidRootPart.Position + cameraLookVector)
  91.         end
  92.     end)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement