Advertisement
Guest User

fly

a guest
Feb 19th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. --
  2. local modules = require(game.ReplicatedStorage.Modules)
  3. local math = modules.Utilities.Math
  4. local utilities = modules.Utilities.Utilities
  5. local UIS = game:GetService("UserInputService")
  6. --
  7.  
  8.  
  9. --[[Variables]]--
  10.  
  11. --Client Objects
  12. local player = game.Players.LocalPlayer
  13. local camera = game.Workspace.CurrentCamera
  14. local char = player.Character
  15.  
  16. --Gui Objects
  17.  
  18. --Animation and Effects
  19. local animate = char:WaitForChild("Animate")
  20. local origin = char:WaitForChild("HumanoidRootPart")
  21. local animFolder = game.ReplicatedStorage.Animations.Flight.Standard
  22. local currentAnim;
  23. local animType;
  24.  
  25. --Control Objects
  26. w,a,s,d = script.W,script.A,script.S,script.D
  27. local lctrl,space = false,false;
  28. local accelerating = script.Accelerating
  29. local flying = script.Flying
  30. local elevating = script.Elevating
  31. local descending = script.Descending
  32.  
  33. --Force Variables
  34. --Flight Velocity and Acceleration
  35. local acc = 1
  36. local min = 5
  37. local max = 50
  38. local dir = 1
  39. local vel = min*dir
  40. local zDir = 1
  41.  
  42. --Turning Velocity (angular velocity on X axis)
  43. local turnA = 0;
  44. local aVel = 3;
  45.  
  46. --Floating System
  47. local frames = 60
  48. local inc = 1/frames
  49. local minOff,maxOff = -.5,.5
  50. local offset = minOff
  51.  
  52. --Ingore List for Raycasting
  53. local Ignore = utilities:GetIgnoreListFromCharacter(char)
  54.  
  55. --Original CFrame
  56. local base = origin.CFrame;
  57.  
  58. --BodyPosition for Movement
  59. local BP = Instance.new("BodyPosition",origin)
  60. BP.Position = base.p
  61. BP.MaxForce = Vector3.new(0,0,0)
  62. BP.D = 1250
  63. BP.P = 10000
  64.  
  65. --BodyGyro for Rotation
  66. local BG = Instance.new("BodyGyro",origin)
  67. BG.MaxTorque = Vector3.new(0,0,0)
  68. BG.D = 1250
  69. BG.P = 5000000
  70.  
  71.  
  72. --[[Functions]]--
  73.  
  74. --Reset (restrict repetitiveness)
  75. local function reset()
  76. w.Value = false
  77. s.Value = false
  78. d.Value = false
  79. a.Value = false
  80. descending.Value = false
  81. elevating.Value = false
  82. accelerating.Value = false
  83. vel = min
  84. end
  85. --
  86.  
  87.  
  88. --[[Events]]--
  89.  
  90. --InputBegan
  91. UIS.InputBegan:connect(function(input,gpe)
  92. if not gpe then
  93. if flying.Value then
  94. local currentInput = tostring(input.KeyCode):lower()
  95. currentInput = currentInput:sub(14,#currentInput)
  96.  
  97. if #currentInput == 1 then
  98. if getfenv(0)[currentInput] ~= nil then
  99. getfenv(0)[currentInput].Value = true
  100. end
  101. elseif input.KeyCode == Enum.KeyCode.LeftControl then
  102. lctrl = true
  103. elseif input.KeyCode == Enum.KeyCode.Space then
  104. space = true
  105. elseif input.KeyCode == Enum.KeyCode.LeftShift then
  106. accelerating.Value = true
  107. end
  108. end
  109. end
  110. end)
  111.  
  112. --InputEnded
  113. UIS.InputEnded:connect(function(input,gpe)
  114. local currentInput = tostring(input.KeyCode):lower()
  115. currentInput = currentInput:sub(14,#currentInput)
  116.  
  117. if #currentInput == 1 and currentInput ~= "q" and currentInput ~= "e" then
  118. if getfenv(0)[currentInput] ~= nil then
  119. getfenv(0)[currentInput].Value = false
  120. end
  121. elseif input.KeyCode == Enum.KeyCode.LeftControl then
  122. lctrl = false
  123. elseif input.KeyCode == Enum.KeyCode.Space then
  124. space = false
  125. elseif input.KeyCode == Enum.KeyCode.LeftShift then
  126. accelerating.Value = false
  127. end
  128. end)
  129.  
  130. --Handle Fly Off/On
  131. flying.Changed:connect(function()
  132. if flying.Value then
  133. base = origin.CFrame * CFrame.new(0,1,0)
  134.  
  135. animate.Disabled = true
  136.  
  137. BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  138. BG.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
  139.  
  140. reset()
  141. else
  142. animate.Disabled = false
  143. accelerating.Value = false
  144.  
  145. BP.MaxForce = Vector3.new(0,0,0)
  146.  
  147. if script.Parent.LockOn.Target.Value == nil then
  148. BG.MaxTorque = Vector3.new(0,0,0)
  149. end
  150.  
  151. reset()
  152. end
  153. end)
  154.  
  155. --Handle Acceleration Effects
  156. accelerating.Changed:connect(function()
  157. if accelerating.Value then
  158. else
  159. end
  160. end)
  161.  
  162.  
  163. --[[Main]]--
  164.  
  165. --Start main function on new thread
  166. utilities.Thread(function()
  167. game:GetService("RunService").RenderStepped:connect(function()
  168. if flying.Value then
  169. local angle = 20;
  170. local lookAt = camera.CFrame.lookVector * 100000
  171. local animation_type = "Idle";
  172.  
  173. aVel = 3;
  174. BP.Position = base.p
  175.  
  176. if space then
  177. if lctrl then
  178. elevating.Value = false
  179. descending.Value = true
  180. else
  181. descending.Value = false
  182. elevating.Value = true
  183. end
  184. else
  185. elevating.Value = false
  186. descending.Value = false
  187. end
  188.  
  189. --Floating
  190. if not w.Value and not a.Value and not s.Value and not d.Value and not elevating.Value and not descending.Value then
  191. vel = min
  192. zDir = 0;
  193. angle = 0;
  194.  
  195. if animType ~= "Idle" and currentAnim then
  196. currentAnim:Stop()
  197. end
  198.  
  199. local n = math.map(math.sin(offset),-1,1,minOff,maxOff)
  200. offset = offset+inc
  201.  
  202. BP.Position = base.p+Vector3.new(0,n,0)
  203. elseif offset ~= minOff then
  204. offset = minOff
  205. end
  206. --
  207.  
  208. --Flying
  209. if w.Value or a.Value or s.Value or d.Value or elevating.Value or descending.Value then
  210. local x,y,z = 0,0,0;
  211. local minY = utilities:CalculateMinimum(origin,Ignore,3.5)
  212. zDir = 0
  213.  
  214. --Elevating
  215. if elevating.Value and not descending.Value then
  216. y = 1
  217.  
  218. animation_type = "Ascend"
  219. if accelerating.Value then
  220. animation_type = "FastAscend"
  221. end
  222. elseif descending.Value and not elevating.Value then
  223. y = -1
  224.  
  225. animation_type = "Descend"
  226. if accelerating.Value then
  227. animation_type = "FastDescend"
  228. end
  229. end
  230. --
  231.  
  232. if w.Value and not s.Value then
  233. z = 1
  234. zDir = -1
  235.  
  236. animation_type = "Forward"
  237. if accelerating.Value then
  238. animation_type = "FastForward"
  239. angle = 90
  240. end
  241. elseif s.Value and not w.Value then
  242. z = -1
  243. zDir = 1
  244. animation_type = "Backward"
  245. end
  246. if a.Value and not d.Value then
  247. x = -1
  248. animation_type = "Left"
  249.  
  250. elseif d.Value and not a.Value then
  251. x = 1
  252. animation_type = "Right"
  253. end
  254. --
  255.  
  256.  
  257. local cY = origin.Position.Y + (y*vel/2)
  258. if cY<minY then
  259. cY = minY
  260. end
  261.  
  262. --Acceleration
  263. if accelerating.Value then
  264. aVel = 10
  265. vel = utilities:CalculateVelocity(vel,dir,acc,min,max)
  266. else
  267. vel = utilities:CalculateVelocity(vel,dir,acc,min,min)
  268. end
  269. --
  270.  
  271. base = CFrame.new(origin.CFrame.p,lookAt) * CFrame.new(x*vel,0,0)
  272. base = base + (base.lookVector*Vector3.new(1,0,1)).unit * (z*vel)
  273. base = base + Vector3.new(0,(y*vel/2),0)
  274.  
  275. if base.p.Y < minY then
  276. base = base - Vector3.new(0,base.p.Y,0) + Vector3.new(0,minY,0)
  277. end
  278.  
  279. BP.Position = base.p
  280. end
  281. --
  282.  
  283. --Animate
  284. if animType ~= animation_type then
  285. animType = animation_type
  286.  
  287. if currentAnim then
  288. currentAnim:Stop()
  289. end
  290.  
  291. currentAnim = char.Humanoid:LoadAnimation(animFolder[animType])
  292. currentAnim:Play()
  293. end
  294. --
  295.  
  296. --Turning
  297. if script.Parent.LockOn.Target.Value == nil then
  298. turnA = utilities:CalculateAngle(turnA,angle*zDir,aVel)
  299. BG.CFrame = CFrame.new(base.p,(lookAt*Vector3.new(1,0,1))) * CFrame.Angles(math.rad(turnA),0,0)
  300. end
  301. --
  302. else
  303. if currentAnim then
  304. currentAnim:Stop()
  305. end
  306. end
  307. end)
  308. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement