sp=script.Parent speed=2 topspeed=50 function waitfor(J,q)while J:FindFirstChild(q)==nil do wait()end return J:FindFirstChild(q)end local J=waitfor(sp,"\099\111\097\115\116\105\110\103\112\111\115\101")local q=waitfor(sp,"\108\101\102\116\116\117\114\110")local Y=waitfor(sp,"\114\105\103\104\116\116\117\114\110")local d=waitfor(sp,"\072\097\110\100\108\101")local Q=waitfor(d,"\087\105\110\100")local G=waitfor(d,"\077\101\115\104")inertia=1-speed/topspeed debris=game:GetService("\068\101\098\114\105\115")equipped=false flying=false controls={forward=0,backward=0,left=0;right=0}momentum=Vector3.new(0,0,0)lastmomentum=Vector3.new(0,0,0)totalmomentum=0 tilt=0 lasttilt=0 currentanim="\099\111\097\115\116"lastanim=Q G.Scale=Vector3.new(3,3,3)d.Size=Vector3.new(2,1.5,3)sp.Grip=CFrame.new(0,0,0)function RemoveFlyStuff()Q:Stop()local J=game.Players.LocalPlayer if J then local q=J.Character if q then for J,q in ipairs(q:getChildren())do if q.Name=="\072\117\109\097\110\111\105\100"then q.PlatformStand=false end if q and q.Name=="\069\102\102\101\099\116\067\108\111\117\100"then q:remove()end end local J=q:FindFirstChild("\084\111\114\115\111")if J~=nil then for J,q in ipairs(J:GetChildren())do if q and(q.Name=="\070\108\105\103\104\116\071\121\114\111"or q.Name=="\070\108\105\103\104\116\086\101\108\111\099\105\116\121")then q:remove()end end end end end d.Transparency=0 d.Smoke.Enabled=true if lastanim~=nil then lastanim:Stop()end end function fly()if not equipped then flying=false else flying=not flying end RemoveFlyStuff()if flying then local G=sp.Parent:FindFirstChild("\084\111\114\115\111")local P=sp.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if G and(P and P.Health>0)then d.Transparency=1 d.Smoke.Enabled=false P.PlatformStand=true momentum=(G.Velocity+G.CFrame.lookVector*10)+Vector3.new(0,3,0)local g=Instance.new("\066\111\100\121\071\121\114\111")g.Name="\070\108\105\103\104\116\071\121\114\111"g.P=1000000.0 g.maxTorque=Vector3.new(g.P,g.P,g.P)g.cframe=G.CFrame g.Parent=G velocity=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")velocity.Name="\070\108\105\103\104\116\086\101\108\111\099\105\116\121"velocity.velocity=Vector3.new(0,0,0)velocity.P=10000.0 velocity.maxForce=Vector3.new(1,1,1)*1000000.0 velocity.Parent=G local t=d:clone()t.Transparency=0 t.CanCollide=false;(waitfor(t,"\083\109\111\107\101")).Enabled=true t.Name="\069\102\102\101\099\116\067\108\111\117\100"local X=Instance.new("\087\101\108\100")X.Part0=G X.Part1=t X.C0=CFrame.new(1,-4,0)*CFrame.Angles(0,math.pi/2,0)X.Parent=t t.Parent=sp.Parent if lastanim~=nil then lastanim:Stop()end lastanim=P:LoadAnimation(J)lastanimused=0 if lastanim then lastanim:Play(.5)end Q:Play()while flying and(G and(P and(P.Health>0 and equipped)))do local d=game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(Vector3.new(controls.left+controls.right,math.abs(controls.forward)*.2,controls.forward+controls.backward))*speed momentum=momentum*inertia+d totalmomentum=math.min(momentum.magnitude,topspeed)local G=totalmomentum/topspeed if t~=nil then local J=t:FindFirstChild("\077\101\115\104")J.Scale=Vector3.new(4,4,4+G*4)end Q.Pitch=G*2+1 Q.Volume=G+.1 local X=((momentum*Vector3.new(1,0,1)).unit:Cross((lastmomentum*Vector3.new(1,0,1)).unit)).y if tostring(X)=="-\049\046\035\073\078\068"or tostring(X)=="\049\046\035\073\078\068"or X==math.huge or X==-math.huge or tostring(0/0)==tostring(X)then X=0 end local L=math.abs(X)if L>.06 or L<.0001 then if math.abs(lasttilt)>.0001 then X=lasttilt*.96 else X=0 end else X=lasttilt*.9+X*.1 end lasttilt=X if X>.01 then if lastanimused~=1 then lastanim:Stop(.5)lastanim=P:LoadAnimation(Y)lastanim:Play(.5)lastanimused=1 end elseif X<-0.01 then if lastanimused~=-1 then lastanim:Stop(.5)lastanim=P:LoadAnimation(q)lastanim:Play(.5)lastanimused=-1 end elseif lastanimused~=0 then lastanim:Stop(.5)lastanim=P:LoadAnimation(J)lastanim:Play(.5)lastanimused=0 end if totalmomentum<.5 then momentum=Vector3.new(0,0,0)totalmomentum=0 g.cframe=CFrame.new(Vector3.new(0,0,0),game.Workspace.CurrentCamera.CoordinateFrame.lookVector*Vector3.new(1,0,1))*CFrame.Angles(0,-math.pi/2,0)else g.cframe=(CFrame.new(Vector3.new(0,0,0),momentum)*CFrame.Angles(0,-math.pi/2,0))*CFrame.Angles(X*-20,0,0)end velocity.velocity=momentum lastmomentum=momentum wait(rate)end end RemoveFlyStuff()flying=false end end function onEquipped(J)equipped=true if J~=nil then J.Icon="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\092\071\117\110\067\117\114\115\111\114\046\112\110\103"J.Button1Down:connect(function()fly()local J=sp.Parent:FindFirstChild("\084\111\114\115\111")if J~=nil then J.Velocity=momentum end end)J.KeyDown:connect(function(J)local q=string.byte(J)if q==32 then elseif q==string.byte("\119")or q==17 then controls.forward=-1 elseif q==string.byte("\097")or q==20 then controls.left=-1 elseif q==string.byte("\115")or q==18 then controls.backward=1 elseif q==string.byte("\100")or q==19 then controls.right=1 end end)J.KeyUp:connect(function(J)local q=string.byte(J)if q==string.byte("\119")or q==17 then controls.forward=0 elseif q==string.byte("\097")or q==20 then controls.left=0 elseif q==string.byte("\115")or q==18 then controls.backward=0 elseif q==string.byte("\100")or q==19 then controls.right=0 end end)end end function onUnequipped()equipped=false end sp.Equipped:connect(onEquipped)sp.Unequipped:connect(onUnequipped)



sp=script.Parent


speed=2
topspeed=50




function waitfor(parent,name)
	while parent:FindFirstChild(name)==nil do
		wait()
	end
	return parent:FindFirstChild(name)
end

local canim=waitfor(sp,"coastingpose")
local lanim=waitfor(sp,"leftturn")
local ranim=waitfor(sp,"rightturn")
local handle=waitfor(sp,"Handle")
local wind=waitfor(handle,"Wind")
local mesh=waitfor(handle,"Mesh")

inertia=1-(speed/topspeed)
debris=game:GetService("Debris")
equipped=false
flying=false
controls={forward=0,backward=0,left=0,right=0}
momentum=Vector3.new(0,0,0)
lastmomentum=Vector3.new(0,0,0)
totalmomentum=0
tilt=0
lasttilt=0
currentanim="coast"
lastanim=wind
mesh.Scale=Vector3.new(3,3,3)
handle.Size=Vector3.new(2,1.5,3)
sp.Grip=CFrame.new(0,0,0)


function RemoveFlyStuff()
	wind:Stop()
	local plr=game.Players.LocalPlayer
	if plr then
		local chr=plr.Character
		if chr then
			for i,v in ipairs(chr:getChildren()) do
				if v.Name=="Humanoid" then
					v.PlatformStand=false
				end
				if v and v.Name=="EffectCloud" then
					v:remove()
				end
			end
			local torso=chr:FindFirstChild("Torso")
			if torso~=nil then
				for i,v in ipairs(torso:GetChildren()) do
					if v and (v.Name=="FlightGyro" or v.Name=="FlightVelocity") then
						v:remove()
					end
				end
			end
		end
	end
	handle.Transparency=0
	handle.Smoke.Enabled=true
	if lastanim~=nil then
		lastanim:Stop()
	end
end

function fly()		--(de)activate fly mode
	if not equipped then
		flying=false
	else
		flying=not flying
	end
	RemoveFlyStuff()
	if flying then
		local torso=sp.Parent:FindFirstChild("Torso")
		local humanoid=sp.Parent:FindFirstChild("Humanoid")
		if torso and humanoid and humanoid.Health>0 then
			handle.Transparency=1
			handle.Smoke.Enabled=false
			humanoid.PlatformStand=true
			momentum=torso.Velocity+(torso.CFrame.lookVector*10)+Vector3.new(0,3,0)

			local gyro=Instance.new("BodyGyro")
			gyro.Name="FlightGyro"
			gyro.P=10^6
			gyro.maxTorque=Vector3.new(gyro.P,gyro.P,gyro.P)
			gyro.cframe=torso.CFrame
			gyro.Parent=torso

			velocity=Instance.new("BodyVelocity")
			velocity.Name="FlightVelocity"
			velocity.velocity=Vector3.new(0,0,0)
			velocity.P=10^4
			velocity.maxForce=Vector3.new(1,1,1)*(10^6)
			velocity.Parent=torso
			
			local cloud=handle:clone()
			cloud.Transparency=0
			cloud.CanCollide=false
			waitfor(cloud,"Smoke").Enabled=true
			cloud.Name="EffectCloud"
			local w=Instance.new("Weld")
			w.Part0=torso
			w.Part1=cloud
			w.C0=CFrame.new(1,-4,0)*CFrame.Angles(0,math.pi/2,0)
			w.Parent=cloud
			cloud.Parent=sp.Parent

			if lastanim~=nil then
				lastanim:Stop()
			end
			lastanim=humanoid:LoadAnimation(canim)
			lastanimused=0
			if lastanim then
				lastanim:Play(.5)
			end
			wind:Play()
			while flying and torso and humanoid and humanoid.Health>0 and equipped do
				local movement=game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(Vector3.new(controls.left+controls.right,math.abs(controls.forward)*.2,controls.forward+controls.backward))*speed

				momentum=(momentum*inertia)+movement
				totalmomentum=math.min(momentum.magnitude,topspeed)
				local momentumpercent=(totalmomentum/topspeed)
				if cloud~=nil then
					local m=cloud:FindFirstChild("Mesh")
					m.Scale=Vector3.new(4,4,4+(momentumpercent*4))
				end
				wind.Pitch=(momentumpercent*2)+1
				wind.Volume=momentumpercent+.1

				local tilt=((momentum*Vector3.new(1,0,1)).unit:Cross(((lastmomentum*Vector3.new(1,0,1)).unit))).y
				if tostring(tilt)=="-1.#IND" or tostring(tilt)=="1.#IND" or tilt==math.huge or tilt==-math.huge or tostring(0/0) == tostring(tilt) then
					tilt=0
				end
				local abstilt=math.abs(tilt)
				if abstilt>.06 or abstilt<.0001 then
					if math.abs(lasttilt)>.0001 then
						tilt=lasttilt*.96
					else
						tilt=0
					end
				else
					tilt=(lasttilt*.9)+(tilt*.1)			--weighted average
				end
				lasttilt=tilt
				if tilt>.01 then
					if lastanimused~=1 then
						lastanim:Stop(.5)
						lastanim=humanoid:LoadAnimation(ranim)
						lastanim:Play(.5)
						lastanimused=1
					end
				elseif tilt<(-.01) then
					if lastanimused~=-1 then
						lastanim:Stop(.5)
						lastanim=humanoid:LoadAnimation(lanim)
						lastanim:Play(.5)
						lastanimused=-1
					end
				elseif lastanimused~=0 then
					lastanim:Stop(.5)
					lastanim=humanoid:LoadAnimation(canim)
					lastanim:Play(.5)
					lastanimused=0
				end
				if totalmomentum<.5 then
					momentum=Vector3.new(0,0,0)
					totalmomentum=0
					gyro.cframe=CFrame.new(Vector3.new(0,0,0),game.Workspace.CurrentCamera.CoordinateFrame.lookVector*Vector3.new(1,0,1))*CFrame.Angles(0,-math.pi/2,0)
				else
					gyro.cframe=CFrame.new(Vector3.new(0,0,0),momentum)*CFrame.Angles(0,-math.pi/2,0)*CFrame.Angles(tilt*(-20),0,0)
				end
				velocity.velocity=momentum
				lastmomentum=momentum
				wait(rate)
			end
		end
		RemoveFlyStuff()
		flying=false
	end
end

function onEquipped(mouse)
	equipped=true
	if mouse~=nil then
		mouse.Icon="rbxasset://textures\\GunCursor.png"
		mouse.Button1Down:connect(function()
			fly()
			local torso=sp.Parent:FindFirstChild("Torso")
			if torso~=nil then
				torso.Velocity=momentum
			end
		end)
		mouse.KeyDown:connect(function(key2)
			local key=string.byte(key2)
			if key==32 then					--Space bar
				--[[fly()
				local torso=sp.Parent:FindFirstChild("Torso")
				if torso~=nil then
					torso.Velocity=momentum
				end]]
			elseif key==string.byte("w") or key==17 then
				controls.forward=-1
			elseif key==string.byte("a") or key==20 then
				controls.left=-1
			elseif key==string.byte("s") or key==18 then
				controls.backward=1
			elseif key==string.byte("d") or key==19 then
				controls.right=1
			end
		end)
		mouse.KeyUp:connect(function(key2)
			local key=string.byte(key2)
			if key==string.byte("w") or key==17 then
				controls.forward=0
			elseif key==string.byte("a") or key==20 then
				controls.left=0
			elseif key==string.byte("s") or key==18 then
				controls.backward=0
			elseif key==string.byte("d") or key==19 then
				controls.right=0
			end
		end)
	end

end

function onUnequipped()
	equipped=false
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)







