Advertisement
Mangus875

Roblox FPV Drone Sim

Jan 14th, 2025 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. local rs = game:GetService("RunService")
  2. local uis = game:GetService("UserInputService")
  3.  
  4. workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
  5. workspace.CurrentCamera.FieldOfView = 100
  6. local drone = workspace.Drone
  7. local deadzone = 0.05
  8.  
  9. local drone = workspace.Drone
  10. local prop = drone.Thrust
  11. local torque = drone.Torque
  12. local gyro = drone.Gyro
  13.  
  14. local rates = {}
  15. -- rates (units: deg/sec at max value)
  16. rates.Yaw = 10
  17. rates.Pitch = 10
  18. rates.Roll = 10
  19.  
  20. local phys = {}
  21. phys.MaxThrust = 3 * drone.Mass * workspace.Gravity
  22. phys.MaxYaw = math.rad(rates.Yaw)
  23. phys.MaxPitch = math.rad(rates.Pitch)
  24. phys.MaxRoll = math.rad(rates.Roll)
  25.  
  26. rs.RenderStepped:Connect(function()
  27.     local inputs = getInputs(deadzone)
  28.  
  29.     local throttle = math.clamp(compDeadzone(inputs.LStick.Y), 0, 1)
  30.     local yawIn = compDeadzone(inputs.LStick.X) * rates.Yaw
  31.     local pitchIn = compDeadzone(inputs.RStick.Y) * rates.Pitch
  32.     local rollIn = compDeadzone(inputs.RStick.X) * rates.Roll
  33.  
  34.     local relLinVel = drone.CFrame:VectorToObjectSpace(drone.AssemblyLinearVelocity)
  35.     local relAngVel = drone.CFrame:VectorToObjectSpace(drone.AssemblyAngularVelocity)
  36.  
  37.     local stab = lerp(throttle, 1, 4) * drone.Mass
  38.     local thrust = throttle * phys.MaxThrust * (1 - math.abs(relLinVel.Y/phys.MaxThrust))
  39.     local pitch = throtToForce(-pitchIn, relAngVel.X, phys.MaxPitch)
  40.     local yaw = throtToForce(-yawIn, relAngVel.Y, phys.MaxYaw)
  41.     local roll = throtToForce(-rollIn, relAngVel.Z, phys.MaxRoll)
  42.  
  43.     print("Fp: "..pitch)
  44.     print("Fy: "..yaw)
  45.     print("Fr: "..roll)
  46.     print("Ip: "..pitchIn)
  47.     print("Iy: "..yawIn)
  48.     print("Ir: "..rollIn)
  49.     print("Vx: "..relAngVel.X)
  50.     print("Vy: "..relAngVel.Y)
  51.     print("Vz: "..relAngVel.Z)
  52.    
  53.     local stabForce = -stab*relAngVel
  54.  
  55.     torque.Torque = Vector3.new(pitch, yaw, roll)
  56.     prop.Force = Vector3.new(0, thrust, 0)
  57.     --gyro.MaxTorque = stab
  58.    
  59.     if inputs.A then
  60.         drone.Position = Vector3.new(0, 0.5, 0)
  61.         drone.Orientation = Vector3.new(0, 90, 0)
  62.         drone.AssemblyLinearVelocity = Vector3.new(0,0,0)
  63.         drone.AssemblyAngularVelocity = Vector3.new(0,0,0)
  64.     end
  65. end)
  66.  
  67. local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
  68. rs.RenderStepped:Connect(function()
  69.     local pos = (drone.CFrame + drone.CFrame.LookVector*0.5 + drone.CFrame.UpVector*0.2)
  70.     workspace.CurrentCamera.CFrame = pos*CFrame.Angles(math.rad(30),0,0)
  71.  
  72.     local inputs = getInputs(deadzone)
  73.     gui.Left.Pos.Position = UDim2.new(inputs.LStick.X/2+0.5,0,-inputs.LStick.Y+1,0)
  74.     gui.Right.Pos.Position = UDim2.new(inputs.RStick.X/2+0.5,0,-inputs.RStick.Y/2+0.5,0)
  75. end)
  76.  
  77. function throtToForce(throttle, vel, max)
  78.     local n = math.abs(vel / throttle)
  79.     n = if n ~= n then 0 else n
  80.     return throttle * (1 - math.clamp(math.pow(n, 2),0,1))
  81. end
  82.  
  83. function lerp(t, a, b)
  84.     return t*(b-a)+a
  85. end
  86.  
  87. local controls = {}
  88. --controls.Primary = {}
  89. --controls.Secondary = {}
  90.  
  91. function getInputs(deadzone:number)
  92.     deadzone = deadzone or 0
  93.     local rawIn:{InputObject} = uis:GetGamepadState(Enum.UserInputType.Gamepad1)
  94.     local inputs = {}
  95.  
  96.     inputs.Menu = rawIn[1].Position.Z == 1
  97.     inputs.Start = rawIn[3].Position.Z == 1
  98.     inputs.A = rawIn[2].Position.Z == 1
  99.     inputs.B = rawIn[4].Position.Z == 1
  100.     inputs.X = rawIn[5].Position.Z == 1
  101.     inputs.Y = rawIn[6].Position.Z == 1
  102.     inputs.LB = rawIn[7].Position.Z == 1
  103.     inputs.RB = rawIn[8].Position.Z == 1
  104.     inputs.LT = rawIn[9].Position.Z
  105.     inputs.RT = rawIn[10].Position.Z
  106.     inputs.DpadUp = rawIn[13].Position.Z == 1
  107.     inputs.DpadDown = rawIn[14].Position.Z == 1
  108.     inputs.DpadLeft = rawIn[15].Position.Z == 1
  109.     inputs.DpadRight = rawIn[16].Position.Z == 1
  110.     inputs.LStick = Vector3.new(applyDeadzone(rawIn[17].Position.X, deadzone), applyDeadzone(rawIn[17].Position.Y, deadzone), rawIn[11])
  111.     inputs.RStick = Vector3.new(applyDeadzone(rawIn[18].Position.X, deadzone), applyDeadzone(rawIn[18].Position.Y, deadzone), rawIn[12])
  112.  
  113.     return inputs
  114. end
  115.  
  116. function getRawInputs(gamepad)
  117.     gamepad = gamepad or Enum.UserInputType.Gamepad1
  118.     local rawIn:{InputObject} = uis:GetGamepadState(gamepad)
  119.     local inputs = {}
  120.  
  121.     inputs.Menu = rawIn[1].Position.Z == 1
  122.     inputs.Start = rawIn[3].Position.Z == 1
  123.     inputs.A = rawIn[2].Position.Z == 1
  124.     inputs.B = rawIn[4].Position.Z == 1
  125.     inputs.X = rawIn[5].Position.Z == 1
  126.     inputs.Y = rawIn[6].Position.Z == 1
  127.     inputs.LB = rawIn[7].Position.Z == 1
  128.     inputs.RB = rawIn[8].Position.Z == 1
  129.     inputs.LT = rawIn[9].Position.Z
  130.     inputs.RT = rawIn[10].Position.Z
  131.     inputs.DpadUp = rawIn[13].Position.Z == 1
  132.     inputs.DpadDown = rawIn[14].Position.Z == 1
  133.     inputs.DpadLeft = rawIn[15].Position.Z == 1
  134.     inputs.DpadRight = rawIn[16].Position.Z == 1
  135.     inputs.LStickX = rawIn[17].Position.X
  136.     inputs.LStickY = rawIn[17].Position.Y
  137.     inputs.LS = rawIn[11]
  138.     inputs.RStickX = rawIn[18].Position.X
  139.     inputs.RStickY = rawIn[18].Position.Y
  140.     inputs.RS = rawIn[12]
  141.  
  142.     return inputs
  143. end
  144.  
  145. function applyDeadzone(val, deadzone)
  146.     local n = if math.abs(val) <= deadzone then 0 else val
  147.    
  148.     return n
  149. end
  150.  
  151. function compDeadzone(input)
  152.     return math.sign(input) * math.map(math.abs(input), deadzone, 1, 0, 1)
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement