So far I have this simple plane flying script here
local UIS = game:GetService("UserInputService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() local shipPart = workspace.TestShip.PrimaryPart local MAX_SPEED = 150 local SPEED = 50 local ACCELERATION = 1 game:GetService("RunService").RenderStepped:Connect(function() shipPart.BodyVelocity.Velocity = workspace.CurrentCamera.CFrame.LookVector * SPEED shipPart.BodyGyro.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position, workspace.CurrentCamera.CFrame.Position + workspace.CurrentCamera.CFrame.LookVector) if UIS:IsKeyDown(Enum.KeyCode.W) then SPEED = math.clamp(SPEED + ACCELERATION, 0, MAX_SPEED) elseif UIS:IsKeyDown(Enum.KeyCode.S) then SPEED = math.clamp(SPEED - (ACCELERATION * 2), 0, MAX_SPEED) elseif UIS:IsKeyDown(Enum.KeyCode.A) then --roll left --shipPart.BodyGyro.CFrame = shipPart.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 90) elseif UIS:IsKeyDown(Enum.KeyCode.D) then --roll right --shipPart.BodyGyro.CFrame = shipPart.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, -90) end end)
The plane will fly in the direction of the camera. I want the plane to roll when the player presses A and D, but I am unsure how to approach this. Can someone help me?
I found a solution
I created a variable called roll
and incremented it by 1 or -1 when A or D is pressed. If roll ever comes equal to either 360 or -360, then it is reset to 0. I then multiplied this angle by the BodyGyro CFrame.
local UIS = game:GetService("UserInputService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() local shipPart = workspace.TestShip.PrimaryPart local MAX_SPEED = 150 local SPEED = 50 local ACCELERATION = 1 local roll = 0 local rollFactor = 3 game:GetService("RunService").RenderStepped:Connect(function() shipPart.BodyVelocity.Velocity = workspace.CurrentCamera.CFrame.LookVector * SPEED shipPart.BodyGyro.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position, workspace.CurrentCamera.CFrame.Position + workspace.CurrentCamera.CFrame.LookVector) if roll == -360 or roll == 360 then roll = 0 end shipPart.BodyGyro.CFrame = shipPart.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, math.rad(roll)) if UIS:IsKeyDown(Enum.KeyCode.W) then SPEED = math.clamp(SPEED + ACCELERATION, 0, MAX_SPEED) elseif UIS:IsKeyDown(Enum.KeyCode.S) then SPEED = math.clamp(SPEED - (ACCELERATION * 2), 0, MAX_SPEED) elseif UIS:IsKeyDown(Enum.KeyCode.A) then --roll left roll = roll + rollFactor elseif UIS:IsKeyDown(Enum.KeyCode.D) then --roll right roll = roll - rollFactor end end)
If someone else thinks they have a better solution please don't hesitate to let me know! Thanks
for left:
elseif UIS:IsKeyDown(Enum.KeyCode.A) then --roll left local TS = game:GetService("TweenService") local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0) local goal = {} goal.Orientation = shipPart.Orientation + Vector3.new(45,0,0) local leftTween = TS:Create(shipPart,tweenInfo,goal)
for right:
elseif UIS:IsKeyDown(Enum.KeyCode.D) then --roll left local TS = game:GetService("TweenService") local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0) local goal = {} goal.Orientation = shipPart.Orientation + Vector3.new(-45,0,0) local leftTween = TS:Create(shipPart,tweenInfo,goal)