I am currently trying to make a plane vehicle that uses only the WASD or the Touch Joystick to move.
My issue comes from using an AlignOrientation to tilt the plane; I want the plane to be able to only have a max Pitch/Roll, and to also re-align itself to be level with the Baseplate if the player is not making any movement inputs.
Here is the code for the plane:
Code
local Plane = script.Parent
local Seat = Plane.VehicleSeat
local Orientation = Seat.AlignOrientation
local Position = Seat.AlignPosition
local Speed = 30
local MaxPitch = 30
local MaxRoll = 70
Seat.Changed:Connect(function()
if Seat.Occupant ~= nil then
Seat.Anchored = false
else
Seat.Anchored = true
end
end)
while task.wait() do
if Seat.Occupant ~= nil then
local Throttle = Seat.Throttle
local Steer = -Seat.Steer
local Pitch = math.rad(Throttle * MaxPitch)
local Roll = math.rad(Steer * MaxRoll)
local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = Orientation.CFrame:GetComponents()
local AppliedPitch = 0
local AppliedYaw = 0
local AppliedRoll = 0
if Steer == 1 then
if Throttle == 1 or Throttle == -1 then
AppliedRoll = Roll
AppliedYaw = Pitch
else
AppliedRoll = Roll
end
elseif Steer == -1 then
if Throttle == 1 or Throttle == -1 then
AppliedRoll = Roll
AppliedYaw = -Pitch
else
AppliedRoll = Roll
end
end
if Throttle ~= 0 and Steer == 0 then
AppliedPitch = Pitch
end
--[[
Line 59 (And I suspect Lines 28-30) are whats causing the issues
As-is, the VehicleSeat re-orients to face north after releasing the keys
If the line is changed to the following:
Orientation.CFrame = Seat.CFrame * CFrame.Angles(AppliedPitch, AppliedYaw, AppliedRoll)
then the AlignOrientation no longer "resets" itself to be level with the ground, and no longer respects the "Max" vars
--]]
Orientation.CFrame = CFrame.Angles(AppliedPitch, AppliedYaw, AppliedRoll)
--Position.Position = (Seat.CFrame + Seat.CFrame.LookVector * Speed).Position --Uncomment this line to move forwards
end
end
As the code is now, here is how the plane acts; Please note how after releasing the keys, specifically when banking/turning, the plane re-orients to face north instead of continuously turning
https://gyazo.com/6c71bc7f05cec48a55a5edbe3969265b
With a minor change to line 59 that adds the seat’s CFrame to the calculation, the “max” variables are no longer taken into account and the plane no longer re-aligns itself, but it does keep the new orientation - though it makes the vehicle near uncontrollable.
https://gyazo.com/f4b863e75adb9a78ac0351d47b938d1c
Is there any way to blend these two behaviors somehow, so that the plane will always re-align itself to be upright, but also keep any new transforms in regards to spinning on the Y Axis?
Here is a copy of the place download so it is easier to view the plane setup and suggest any changes I may need to make.
PlaneTest.rbxl (48.0 KB)
Thank you!