local Wing = script.Parent:WaitForChild("LargeWing") --The actual wing Part
local FlightAttachment = Wing:WaitForChild("FlightAttachment") --attachment used to get the wing's current position, and offset the Y
local Flight = Wing:WaitForChild("Flight") --A BodyVelocity
local VehicleMass
local Chair = script.Parent.Parent:WaitForChild("Chair"):WaitForChild("Chair")--Master controller for entire vehicle
local Drag = script.Parent:GetAttribute("Drag")--Arbitrary attribute value. currently at 20
local LiftPower = script.Parent:GetAttribute("LiftPower") --Arbitrary attribute value, used to increase the amount of gliding the plane can do. More lift power = less speed plane needs to fly. currently at 1.06
local FlyingModule = require(script.Parent.Parent:WaitForChild("FlyingModule"))
local GlideAttachment = Wing:WaitForChild("GlideAttachment")--Same thing as Flight Attachment, but offsets the Z instead of Y
local Glide = Wing:WaitForChild("Glide")--A BodyVelocity
local function CalculateMassAndWings()
VehicleMass = FlyingModule.VehicleFlyingStats("Get","Mass")
end
CalculateMassAndWings() --Gets the mass of the vehicle
script.Parent.Parent.ChildAdded:Connect(CalculateMassAndWings)
script.Parent.Parent.ChildRemoved:Connect(CalculateMassAndWings)--If any part of the vehicle breaks off, recalculate mass
function CalculateNumberOfActiveWings()--Calculates the amount of wings that are current applying physics
local NumberOfActiveWings=0
local Wings = FlyingModule.VehicleFlyingStats("Get","Wings")
for x=1,#Wings do
if Wings[x]:GetAttribute("Active") then
NumberOfActiveWings=NumberOfActiveWings+1
end
end
return NumberOfActiveWings
end
while script.Parent:GetAttribute("Health")>0 do
wait()
local XOri= Wing.Orientation.X--Grabs X and Z orientation, and runs it through logic. The resulting outcome is a percentage of how useful the wing is. The more tilted a wing is, the less efficient it is
local ZOri=Wing.Orientation.Z
local ZE,XE=0,0
if ZOri <=180 and ZOri>=120 then
ZE=math.abs((ZOri-120)/(180-120))
elseif ZOri<=60 and ZOri>=0 then
ZE=math.abs((ZOri-60)/(0-60))
elseif ZOri>=-60 and ZOri <=-.000003 then
ZE=math.abs((ZOri-(-60))/(-000003-(-60)))
elseif ZOri>=-180 and ZOri<=-120 then
ZE=math.abs((ZOri-(-120))/(-180-(-120)))
else
print("Wing is too tilted to fly on z axis: ",ZOri)
end
if XOri <=0 and XOri>= -60 then
XE=math.abs((XOri-(-60))/(0-(-60)))
elseif XOri>=0 and XOri<=60 then
XE=math.abs((XOri-60)/(0-60))
else
print("Wing is too tilted to fly on the x axis: ",XOri)
end
local WingEfficiency
if XE>ZE then --Finally, the lowest value is chosen as the WingEfficiency to be used in the formula
WingEfficiency=ZE
else
WingEfficiency=XE
end
local Speed = Chair.Velocity.magnitude
if WingEfficiency>0 and Speed >= 40 then --If the vehicle is fast enough (40), and the wings aren't at completely terrible angles, apply physics
script.Parent:SetAttribute("Active",true)--Report the wing to be active
local NumberOfActiveWings=CalculateNumberOfActiveWings() --Find out how many other wings are also applying physics
local MaxForce = VehicleMass/NumberOfActiveWings--The maximum force a wing should apply UPWARDS. Too little, the plane will fall out of the sky. too much, and the wings will actually LIFT the plane striaght up
local Power=(20+(1.1*LiftPower)^(Speed-20))*WingEfficiency--exponential formula that compares speed and wing efficiency.
local NewSpeed = -Speed/NumberOfActiveWings--The speed of the vehicle, divided by the amount of wings currently active. Its set negative because this value is what propells the plane forwards, not backwards
if NewSpeed<-Drag then --if the value is big enough to handle drag, take drag from value. this is to prevent the vehicle from immediately flying backwards
NewSpeed=NewSpeed+Drag
end
if Power > MaxForce then --the Power cannot exceed MaxForce
Power=MaxForce
end
FlightAttachment.WorldPosition = Vector3.new(Wing.Position.X,Wing.Position.Y+Power,Wing.Position.Z)--Offset the Flight Attachment by itself and the Power. This allows the wings to engage anti-gravity.
GlideAttachment.Position=Vector3.new(0,0,NewSpeed)--Same thing as Flight Attachment, but instead of Y, its Z. This is to pull the plane forwards and prevent it from spinning aimlessly
Glide.Velocity = Vector3.new(0,0,GlideAttachment.WorldPosition.Z)--Idk what velocity even means. So I set it to Glide's Z position. Hopefully, the BodyVelocity pulls towards that point constant. (it doesnt.)
Flight.Velocity= Vector3.new(0,FlightAttachment.WorldPosition.Y,GlideAttachment.WorldPosition.Z)--Pull the plane upwards? Kinda works.
Glide.MaxForce=Vector3.new(0,0,Power)--Idk whats the real difference between this and P, but P seems to break the universe, so I'll stick with MaxForce
Flight.MaxForce=Vector3.new(0,Power,Power/NumberOfActiveWings)
else
script.Parent:SetAttribute("Active",false)--turn off wing is its too tilted or slow
Flight.MaxForce = Vector3.new(0,0,0)
Glide.MaxForce=Vector3.new(0,0,0)
end
end
I commentated as much as I could so you didn’t have to decipher anything.
The flying upwards part (lift) seems to work fine enough. But if the plane bumps into anything, it spins aimlessly. It basically makes planes drift in mid air. AND, flying straight for too long makes the plane exponentially go faster. I imagine this is because Im pulling the plane forwards by its own speed, which makes it go faster and faster. But idk what would be a proper substitute!