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

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?

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.

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

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

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)
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