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

How to control airplane with mouse?

Asked by 2 years ago

I am trying to make a basic airplane that uses the player's mouse to steer the vehicle. I've tried for hours but I can't find any solution...

I have only figured out how to move one part using my mouse, I can't get the plane models' children (every part of the plane) to move according to its speed and the mouse position, please help.

Here is my code:

local seat = script.Parent
local front_speed = 1
local up_speed = 0
local max_height = 100
local throttle = 0
local throttleMax = 100
local throttleDown = false
local throttleUp = false
local UserInputService = game:GetService("UserInputService")


while task.wait() do

    local TweenService = game:GetService("TweenService")


    script.AngularVelocity.AngularVelocity = Vector3.new(0, -1 * seat.Steer, 0)

    print(workspace.Plane2.mainbase.VehicleSeat.Position.Y) -- equal to (20^2 / 2*10) = 400/20 = 20 studs


    if seat.Throttle == 1 then

        throttleUp = true

        d = 1
        repeat

            d = d + 1

            script.LinearVelocity.VectorVelocity = Vector3.new(0, up_speed, -front_speed)

            front_speed = front_speed * 1.05
            wait(0.1)
            if front_speed >= 100 then
                up_speed = 5
            end
            if front_speed < 100 then
                up_speed = 0
            end
            if front_speed > 500 then
                script.LinearVelocity.VectorVelocity = Vector3.new(0, up_speed, -500)
            end

            print(d)
        until d == 100 or seat.Throttle == 0 or seat.Throttle == -1 or front_speed == 500

        script.LinearVelocity.VectorVelocity = Vector3.new(0, up_speed, -front_speed)


        --[[d = 1
        repeat

            d = d + 1
            for _, v in pairs(script.Parent.Parent.Parent:GetChildren()) do

                v.Orientation = Vector3.new(0,0,d)  
            end
            wait(0.1)
        until d == 20
        ]]

    end


    if seat.Throttle == -1 then
        repeat

            d = d - 1

            script.LinearVelocity.VectorVelocity = Vector3.new(0, -up_speed, -front_speed)

            front_speed = front_speed * 0.95
            wait(0.1)

            if front_speed > 100 and up_speed > 1 then
                up_speed = -5
            end
            if front_speed == 0 then
                d = 0
            end

            print(d)
        until d == 100 or seat.Throttle == 0 or seat.Throttle == -1 or front_speed == 500

        script.LinearVelocity.VectorVelocity = Vector3.new(0, -up_speed, -front_speed)

    end
    if seat.Throttle == 0 then

        script.LinearVelocity.VectorVelocity = Vector3.new(0, 0, -front_speed)

    end

    if seat.Throttle ~= 1 and seat.Throttle ~= 0 and math.round(seat.Position.Y - seat.Size.Y / 2) < -80 then

        c = 1
        repeat
            script.LinearVelocity.VectorVelocity = Vector3.new(0, 0, -front_speed - 1)
            front_speed = front_speed - 1
            c = c + 1
            wait(0.2)

            if seat.Throttle == 1 then

                script.LinearVelocity.VectorVelocity = Vector3.new(0, up_speed, -front_speed)

                break
            end
        until c == 100


    end

    if seat.Position.Y + seat.Size.Y / 2 > max_height then

        script.LinearVelocity.VectorVelocity = Vector3.new(0, -up_speed, -front_speed)

    end

    if seat.Occupant == nil then
        script.LinearVelocity.VectorVelocity = Vector3.new(0, 0, 0)
    end

end

        mouse.Move:connect(function()
    script.Parent.Parent.Parent:GetChildren().Position = mouse.Hit.p 
end)

Answer this question