Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- local modules = require(game.ReplicatedStorage.Modules)
- local math = modules.Utilities.Math
- local utilities = modules.Utilities.Utilities
- local UIS = game:GetService("UserInputService")
- --
- --[[Variables]]--
- --Client Objects
- local player = game.Players.LocalPlayer
- local camera = game.Workspace.CurrentCamera
- local char = player.Character
- --Gui Objects
- --Animation and Effects
- local animate = char:WaitForChild("Animate")
- local origin = char:WaitForChild("HumanoidRootPart")
- local animFolder = game.ReplicatedStorage.Animations.Flight.Standard
- local currentAnim;
- local animType;
- --Control Objects
- w,a,s,d = script.W,script.A,script.S,script.D
- local lctrl,space = false,false;
- local accelerating = script.Accelerating
- local flying = script.Flying
- local elevating = script.Elevating
- local descending = script.Descending
- --Force Variables
- --Flight Velocity and Acceleration
- local acc = 1
- local min = 5
- local max = 50
- local dir = 1
- local vel = min*dir
- local zDir = 1
- --Turning Velocity (angular velocity on X axis)
- local turnA = 0;
- local aVel = 3;
- --Floating System
- local frames = 60
- local inc = 1/frames
- local minOff,maxOff = -.5,.5
- local offset = minOff
- --Ingore List for Raycasting
- local Ignore = utilities:GetIgnoreListFromCharacter(char)
- --Original CFrame
- local base = origin.CFrame;
- --BodyPosition for Movement
- local BP = Instance.new("BodyPosition",origin)
- BP.Position = base.p
- BP.MaxForce = Vector3.new(0,0,0)
- BP.D = 1250
- BP.P = 10000
- --BodyGyro for Rotation
- local BG = Instance.new("BodyGyro",origin)
- BG.MaxTorque = Vector3.new(0,0,0)
- BG.D = 1250
- BG.P = 5000000
- --[[Functions]]--
- --Reset (restrict repetitiveness)
- local function reset()
- w.Value = false
- s.Value = false
- d.Value = false
- a.Value = false
- descending.Value = false
- elevating.Value = false
- accelerating.Value = false
- vel = min
- end
- --
- --[[Events]]--
- --InputBegan
- UIS.InputBegan:connect(function(input,gpe)
- if not gpe then
- if flying.Value then
- local currentInput = tostring(input.KeyCode):lower()
- currentInput = currentInput:sub(14,#currentInput)
- if #currentInput == 1 then
- if getfenv(0)[currentInput] ~= nil then
- getfenv(0)[currentInput].Value = true
- end
- elseif input.KeyCode == Enum.KeyCode.LeftControl then
- lctrl = true
- elseif input.KeyCode == Enum.KeyCode.Space then
- space = true
- elseif input.KeyCode == Enum.KeyCode.LeftShift then
- accelerating.Value = true
- end
- end
- end
- end)
- --InputEnded
- UIS.InputEnded:connect(function(input,gpe)
- local currentInput = tostring(input.KeyCode):lower()
- currentInput = currentInput:sub(14,#currentInput)
- if #currentInput == 1 and currentInput ~= "q" and currentInput ~= "e" then
- if getfenv(0)[currentInput] ~= nil then
- getfenv(0)[currentInput].Value = false
- end
- elseif input.KeyCode == Enum.KeyCode.LeftControl then
- lctrl = false
- elseif input.KeyCode == Enum.KeyCode.Space then
- space = false
- elseif input.KeyCode == Enum.KeyCode.LeftShift then
- accelerating.Value = false
- end
- end)
- --Handle Fly Off/On
- flying.Changed:connect(function()
- if flying.Value then
- base = origin.CFrame * CFrame.new(0,1,0)
- animate.Disabled = true
- BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
- BG.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
- reset()
- else
- animate.Disabled = false
- accelerating.Value = false
- BP.MaxForce = Vector3.new(0,0,0)
- if script.Parent.LockOn.Target.Value == nil then
- BG.MaxTorque = Vector3.new(0,0,0)
- end
- reset()
- end
- end)
- --Handle Acceleration Effects
- accelerating.Changed:connect(function()
- if accelerating.Value then
- else
- end
- end)
- --[[Main]]--
- --Start main function on new thread
- utilities.Thread(function()
- game:GetService("RunService").RenderStepped:connect(function()
- if flying.Value then
- local angle = 20;
- local lookAt = camera.CFrame.lookVector * 100000
- local animation_type = "Idle";
- aVel = 3;
- BP.Position = base.p
- if space then
- if lctrl then
- elevating.Value = false
- descending.Value = true
- else
- descending.Value = false
- elevating.Value = true
- end
- else
- elevating.Value = false
- descending.Value = false
- end
- --Floating
- 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
- vel = min
- zDir = 0;
- angle = 0;
- if animType ~= "Idle" and currentAnim then
- currentAnim:Stop()
- end
- local n = math.map(math.sin(offset),-1,1,minOff,maxOff)
- offset = offset+inc
- BP.Position = base.p+Vector3.new(0,n,0)
- elseif offset ~= minOff then
- offset = minOff
- end
- --
- --Flying
- if w.Value or a.Value or s.Value or d.Value or elevating.Value or descending.Value then
- local x,y,z = 0,0,0;
- local minY = utilities:CalculateMinimum(origin,Ignore,3.5)
- zDir = 0
- --Elevating
- if elevating.Value and not descending.Value then
- y = 1
- animation_type = "Ascend"
- if accelerating.Value then
- animation_type = "FastAscend"
- end
- elseif descending.Value and not elevating.Value then
- y = -1
- animation_type = "Descend"
- if accelerating.Value then
- animation_type = "FastDescend"
- end
- end
- --
- if w.Value and not s.Value then
- z = 1
- zDir = -1
- animation_type = "Forward"
- if accelerating.Value then
- animation_type = "FastForward"
- angle = 90
- end
- elseif s.Value and not w.Value then
- z = -1
- zDir = 1
- animation_type = "Backward"
- end
- if a.Value and not d.Value then
- x = -1
- animation_type = "Left"
- elseif d.Value and not a.Value then
- x = 1
- animation_type = "Right"
- end
- --
- local cY = origin.Position.Y + (y*vel/2)
- if cY<minY then
- cY = minY
- end
- --Acceleration
- if accelerating.Value then
- aVel = 10
- vel = utilities:CalculateVelocity(vel,dir,acc,min,max)
- else
- vel = utilities:CalculateVelocity(vel,dir,acc,min,min)
- end
- --
- base = CFrame.new(origin.CFrame.p,lookAt) * CFrame.new(x*vel,0,0)
- base = base + (base.lookVector*Vector3.new(1,0,1)).unit * (z*vel)
- base = base + Vector3.new(0,(y*vel/2),0)
- if base.p.Y < minY then
- base = base - Vector3.new(0,base.p.Y,0) + Vector3.new(0,minY,0)
- end
- BP.Position = base.p
- end
- --
- --Animate
- if animType ~= animation_type then
- animType = animation_type
- if currentAnim then
- currentAnim:Stop()
- end
- currentAnim = char.Humanoid:LoadAnimation(animFolder[animType])
- currentAnim:Play()
- end
- --
- --Turning
- if script.Parent.LockOn.Target.Value == nil then
- turnA = utilities:CalculateAngle(turnA,angle*zDir,aVel)
- BG.CFrame = CFrame.new(base.p,(lookAt*Vector3.new(1,0,1))) * CFrame.Angles(math.rad(turnA),0,0)
- end
- --
- else
- if currentAnim then
- currentAnim:Stop()
- end
- end
- end)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement