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

How To Move Helicopter?

Asked by 4 years ago
Edited 4 years ago

Hello fellow devs. I've run into something. I wrote this script which handles moving of part perfectly like , but I don't take turning into account.

    -- Made By GooierApollo664 
    -- 11/07/2019

    -- Services
    local UserInputService = game:GetService("UserInputService")
    local Players = game:GetService("Players")

    -- Variables 
    local Heli = script.Parent.Parent
    local GyroScope = script.Parent
    local Thrust = Heli:FindFirstChild("BodyThrust")
    local Reaction = Heli:FindFirstChild("BodyReaction")
    local Force = Heli:FindFirstChild("BodyForce")

    -- Physics Variables
    local m = Heli:GetMass()
    local g =  workspace.Gravity
    local cf = CFrame.new()
    local vector = Vector3.new()

    -- Other 
    local enum = Enum.KeyCode

    -- Movement Table
    local Movement = {[tostring(enum.T)] = CFrame.Angles(0, 0, math.rad(20)), 
                      [tostring(enum.F)] = CFrame.Angles(math.rad(10), 0, 0),    
                      [tostring(enum.H)] = CFrame.Angles(math.rad(-10), 0, 0),
                      [tostring(enum.G)] = CFrame.Angles(0, 0, math.rad(-20)),
                      [tostring(enum.Y)] = Vector3.new(0, 100, 0),
                      [tostring(enum.R)] = Vector3.new(0, -100, 0)
                     }
    -- Constants
    local Keys = {[tostring(enum.T)] = cf,
                  [tostring(enum.F)] = cf,
                  [tostring(enum.G)] = cf,
                  [tostring(enum.H)] = cf,
                  [tostring(enum.Y)] = vector,
                  [tostring(enum.R)] = vector
                 }

    -- The loop 
    game:GetService("RunService").Stepped:Connect(function()

        local W = Keys[tostring(enum.T)] 
        local A = Keys[tostring(enum.F)] 
        local D = Keys[tostring(enum.G)] 
        local S = Keys[tostring(enum.H)] 
        local Y = Keys[tostring(enum.Y)] 
        local R = Keys[tostring(enum.R)] 

        -- Gravity work
        Thrust.Force = Vector3.new(0, 2*m*g, 0)
        Reaction.Force = -Vector3.new(0, m*g, 0)

        Heli.Velocity = Heli.Velocity - Heli.Velocity/10

        -- adding Force values
        Force.Force = R + Y 
        GyroScope.CFrame = W * A * S * D
    end)

    UserInputService.InputBegan:Connect(function(input)

        Keys[tostring(input.KeyCode)] = Movement[tostring(input.KeyCode)]

    end)

    UserInputService.InputEnded:Connect(function(input)

        Keys[tostring(input.KeyCode)] = cf 

    end)

https://gyazo.com/3b81764428846bf1bf6ebf1d26342a7b

So I added few things to this code to make it turn using BodyAngularVelocity.

    local AngularVelocity = Heli:FindFirstChild("BodyAngularVelocity")

    -- Physics Variables
    local m = Heli:GetMass()
    local g =  workspace.Gravity
    local cf = CFrame.new()
    local vector = Vector3.new()

    -- Other 
    local enum = Enum.KeyCode

    -- Movement Table
    local Movement = {-- leaning Forward, Left, Right and back
                      [tostring(enum.T)] = {CFrame.Angles(0, 0, math.rad(-20)), Vector3.new(0,(m*g)/27, 0)}, 
                      [tostring(enum.R)] = {CFrame.Angles(math.rad(10), 0, 0), Vector3.new(0,(m*g)/67, 0)},  
                      [tostring(enum.Y)] = {CFrame.Angles(math.rad(-10), 0, 0), Vector3.new(0,(m*g)/67, 0)},
                      [tostring(enum.G)] = {CFrame.Angles(0, 0, math.rad(20)), Vector3.new(0,(m*g)/17,0)},
                      -- Left And Right turn with angular velocity
                      [tostring(enum.H)] = {Vector3.new(0, -2, 0), 0},
                      [tostring(enum.F)] = {Vector3.new(0,2, 0), 0},
                      -- Up and Down
                      [tostring(enum.J)] = {Vector3.new(0, 100, 0), 0},
                      [tostring(enum.LeftShift)] = {Vector3.new(0, -100, 0), 0}
                     }

    local Keys = {-- leaning Forward, Left, Right and back
                  [tostring(enum.T)] = {cf, vector},
                  [tostring(enum.R)] = {cf, vector},
                  [tostring(enum.Y)] = {cf, vector},
                  [tostring(enum.G)] = {cf, vector},
                  -- Left And Right turn with angular velocity
                  [tostring(enum.H)] = {vector, vector},
                  [tostring(enum.F)] = {vector, vector},
                  -- -- Up and Down     
                  [tostring(enum.J)] = {vector, vector},    
                  [tostring(enum.LeftShift)] = {vector, vector}
                 }

    -- Constants
    local Nulls = {-- leaning Forward, Left, Right and back
                   [tostring(enum.T)] = {cf, vector},
                   [tostring(enum.R)] = {cf, vector},
                   [tostring(enum.Y)] = {cf, vector},
                   [tostring(enum.G)] = {cf, vector},
                   -- Left And Right turn with angular velocity
                   [tostring(enum.F)] = {vector, vector},
                   [tostring(enum.H)] = {vector, vector},
                   -- Up and Down
                   [tostring(enum.J)] = {vector, vector},   
                   [tostring(enum.LeftShift)] = {vector, vector}            
                  }




    -- The loop 
    game:GetService("RunService").Stepped:Connect(function()
        local Position = Heli.Position
        local dir = Heli.CFrame.LookVector

        local W = Keys[tostring(enum.T)]
        local A = Keys[tostring(enum.F)] 
        local S = Keys[tostring(enum.G)]
        local D = Keys[tostring(enum.H)] 
        local Y = Keys[tostring(enum.Y)] 
        local R = Keys[tostring(enum.R)] 
        local J = Keys[tostring(enum.J)]
        local Shift = Keys[tostring(enum.LeftShift)]

        -- Reaction Force
        Thrust.Force = Vector3.new(0, 2*m*g, 0)
        Reaction.Force = -Vector3.new(0, m*g, 0)

        -- Friction/Resistance
        Heli.Velocity = Heli.Velocity - Heli.Velocity/10

        -- adding Force/CFrame values
        AngularVelocity.AngularVelocity =  A[1] + D[1]
        Force.Force = J[1] + Shift[1] + W[2] + S[2] + R[2] + Y[2] 

        GyroScope.CFrame = W[1] * S[1] * R[1] * Y[1]
    end)

    UserInputService.InputBegan:Connect(function(input)

        Keys[tostring(input.KeyCode)] = Movement[tostring(input.KeyCode)]

    end)

    UserInputService.InputEnded:Connect(function(input)

        Keys[tostring(input.KeyCode)] = Nulls[tostring(input.KeyCode)]

    end)

https://gyazo.com/af5d7a647b6b5968c30082a264119ba2

Here I added another Table to hold nulls, that I call values which should be applied when helicopter is stopped(or no key is pressed, to be precise). The problem I have to solve, is that I should rotate Gyro based on lookVector and constantly update it, but I have trouble figuring out how to do it with this code. I came up with this solution

    function Cframe(x, z) 

        if x or z then

            return CFrame.Angles(x, 0, z)
        else
            return CFrame.Angles(0, 0, 0)
        end

    end

this is the function I made which would replace cframe.Angles in tables.

    local Movement = {-- leaning Forward, Left, Right and back
                      [tostring(enum.T)] = {Cframe, Vector3.new(0,(m*g)/27, 0)}, 
                      [tostring(enum.R)] = {Cframe, Vector3.new(0,(m*g)/67, 0)},     
                      [tostring(enum.Y)] = {Cframe, Vector3.new(0,(m*g)/67, 0)},
                      [tostring(enum.G)] = {Cframe, Vector3.new(0,(m*g)/17,0)},
                      -- Left And Right
                      [tostring(enum.H)] = {Vector3.new(0, -2, 0), 0},
                      [tostring(enum.F)] = {Vector3.new(0,2, 0), 0},
                      -- Up and Down
                      [tostring(enum.J)] = {Vector3.new(0, 100, 0), 0},
                      [tostring(enum.LeftShift)] = {Vector3.new(0, -100, 0), 0}
                     }

    local Keys = {-- leaning Forward, Left, Right and back
                  [tostring(enum.T)] = {cf, vector},
                  [tostring(enum.R)] = {cf, vector},
                  [tostring(enum.Y)] = {cf, vector},
                  [tostring(enum.G)] = {cf, vector},
                  -- Left And Right turn with angular velocity
                  [tostring(enum.H)] = {vector, vector},
                  [tostring(enum.F)] = {vector, vector},
                  -- -- Up and Down     
                  [tostring(enum.J)] = {vector, vector},    
                  [tostring(enum.LeftShift)] = {vector, vector}
                 }

    -- Constants
    local Nulls = {-- leaning Forward, Left, Right and back
                   [tostring(enum.T)] = {cf, vector},
                   [tostring(enum.R)] = {cf, vector},
                   [tostring(enum.Y)] = {cf, vector},
                   [tostring(enum.G)] = {cf, vector},
                   -- Left And Right turn with angular velocity
                   [tostring(enum.F)] = {vector, vector},
                   [tostring(enum.H)] = {vector, vector},
                   -- Up and Down
                   [tostring(enum.J)] = {vector, vector},   
                   [tostring(enum.LeftShift)] = {vector, vector}            
                  }


    local dir = heli.CFrame.LookVector

    local W = Keys[tostring(enum.T)]
    local S = Keys[tostring(enum.G)]
    local Y = Keys[tostring(enum.Y)]
    local R = Keys[tostring(enum.R)]

    GyroScope.CFrame = W[1](-(dir.X), -(dir.Z)) * S[1](dir.X, dir.Z) * R[1] * Y[1]


    UserInputService.InputBegan:Connect(function(input)

        Keys[tostring(input.KeyCode)] = Movement[tostring(input.KeyCode)]

    end)

    UserInputService.InputEnded:Connect(function(input)

        Keys[tostring(input.KeyCode)] = Nulls[tostring(input.KeyCode)]

    end) 

The problem with this is, that if I release W key or S Key, in Nulls Table first element is just CFrame.new() so passing arguments like that is not possible.

Maybe there's some methods I don't know(which there clearly are), or I'm missing something obvious. Note:I use T, R, Y, G, H, F and J keys in tables, because they are not available in studio Run test mode.

0
Can I please have a nice detailed view of the "Heli" part in explorer tab? Make sure to open all the folders, so I can see what is where. Arj783 72 — 4y
0
Idk what kind of math you have going on right now but all you have to do is use a body position and reference the main part's positing.X + a few studs and just make it move like that namespace25 594 — 4y
0
He wants it to turn, not to strafe Sir_Ragealot 72 — 4y
0
This would be a lot easier if you used modern body movers and not legacy Sir_Ragealot 72 — 4y

1 answer

Log in to vote
0
Answered by 3 years ago

Maybe try an animation??

Ad

Answer this question