Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] How to implement roll in aircraft flight script?

Asked by
blazar04 281 Moderation Voter
4 years ago
Edited 4 years ago

So far I have this simple plane flying script here

01local UIS = game:GetService("UserInputService")
02local player = game.Players.LocalPlayer
03local mouse = player:GetMouse()
04local shipPart = workspace.TestShip.PrimaryPart
05 
06local MAX_SPEED = 150
07local SPEED = 50
08local ACCELERATION = 1
09 
10game:GetService("RunService").RenderStepped:Connect(function()
11 
12    shipPart.BodyVelocity.Velocity = workspace.CurrentCamera.CFrame.LookVector * SPEED
13    shipPart.BodyGyro.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position, workspace.CurrentCamera.CFrame.Position + workspace.CurrentCamera.CFrame.LookVector)
14 
15    if UIS:IsKeyDown(Enum.KeyCode.W) then
View all 26 lines...

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?

2 answers

Log in to vote
0
Answered by
blazar04 281 Moderation Voter
4 years ago
Edited 4 years ago

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.

01local UIS = game:GetService("UserInputService")
02local player = game.Players.LocalPlayer
03local mouse = player:GetMouse()
04local shipPart = workspace.TestShip.PrimaryPart
05 
06local MAX_SPEED = 150
07local SPEED = 50
08local ACCELERATION = 1
09 
10local roll = 0
11local rollFactor = 3
12 
13game:GetService("RunService").RenderStepped:Connect(function()
14 
15    shipPart.BodyVelocity.Velocity = workspace.CurrentCamera.CFrame.LookVector * SPEED
View all 32 lines...

If someone else thinks they have a better solution please don't hesitate to let me know! Thanks

Ad
Log in to vote
-1
Answered by 4 years ago

for left:

1elseif UIS:IsKeyDown(Enum.KeyCode.A) then
2        --roll left
3    local TS = game:GetService("TweenService")
4 
5    local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0)
6    local goal = {}
7    goal.Orientation = shipPart.Orientation + Vector3.new(45,0,0)  
8 
9    local leftTween = TS:Create(shipPart,tweenInfo,goal)

for right:

1elseif UIS:IsKeyDown(Enum.KeyCode.D) then
2        --roll left
3    local TS = game:GetService("TweenService")
4 
5    local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0)
6    local goal = {}
7    goal.Orientation = shipPart.Orientation + Vector3.new(-45,0,0
8 
9    local leftTween = TS:Create(shipPart,tweenInfo,goal)
0
The tweening is unnecessary in this case since the code is ran every frame under RenderStepped, but I can instead just change the orientation of the part incrementally. I didn't even think of that for some reason, I guess I was just overthinking blazar04 281 — 4y
0
However, I think I need to rotate the BodyGyro not the shipPart itself... I will leave this open for more answers for now blazar04 281 — 4y
0
Also, I forgot to mention that since the ship will be comprised of multiple parts, the orientation of each of the parts will be different. Using this approach would cause all parts to have the same orientation, messing up the model blazar04 281 — 4y
0
oh ok ProjectInfiniti 192 — 4y

Answer this question