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

How to maintain momentum in a fly script?

Asked by
stepatron 103
5 years ago
-- Services
local input = game:GetService("UserInputService")  -- Gets input from player's keyboard
local rs = game:GetService("RunService")           -- Handles the runtime of the game
local camera = game.Workspace.CurrentCamera

-- Player
local player = game:GetService("Players").LocalPlayer               -- Player
local character                                                     -- Player's character
local root                                                          -- Character's HumanoidRootPart
local humanoid                                                      -- Character's Humanoid
player.CharacterAdded:connect(function(Char)
    character = Char
    root = Char:WaitForChild("HumanoidRootPart")                    
    humanoid = Char:WaitForChild("Humanoid")                    
end)

-- Body Properties
local bodyVel = Instance.new("BodyVelocity", script)                -- Create "BodyVelocity" in rootpart
bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)     -- Infinite potential forces
bodyVel.Velocity = Vector3.new()                                    -- No velocity / not moving

-- Directional Values
local xDelta = 0                                                    -- 1/0/-1 = left/not moving/right
local lastXDelta = 0
local zDelta = 0                                                    -- 1/0/-1 = forwards/not moving/backwards
local lastZDelta = 0

-- Speed Settings
local maxSpeed = 200                                                -- Maximum total speed
local currentSpeed = 0                                              -- Current flying speed

-- Current Flying Status
local flying = false


-- Activate/Deactivate Flying
input.InputBegan:connect(function(keyPut)
    local key = keyPut.KeyCode                                      -- What key was pressed
    if key == Enum.KeyCode.F and flying == false then
        bodyVel.Parent = root
        bodyVel.Velocity = root.Velocity
        flying = true                                               -- Activate flying
    elseif key == Enum.KeyCode.F and flying == true then
        bodyVel.Parent = script
        flying = false
    end
end)

-- Flying controls
input.InputBegan:connect(function(keyPut)
    local key = keyPut.KeyCode

    if key == Enum.KeyCode.W then           -- Forward Movement
        zDelta = zDelta - 1
    elseif key == Enum.KeyCode.A then       -- Left Movement
        xDelta = xDelta - 1
    elseif key == Enum.KeyCode.S then       -- Backwards Movement
        zDelta = zDelta + 1
    elseif key == Enum.KeyCode.D then       -- Right Movement
        xDelta = xDelta + 1
    end
end)

input.InputEnded:connect(function(keyPut)
    local key = keyPut.KeyCode
    if key == Enum.KeyCode.W then
        zDelta = zDelta + 1
    elseif key == Enum.KeyCode.A then
        xDelta = xDelta + 1
    elseif key == Enum.KeyCode.S then
        zDelta = zDelta - 1
    elseif key == Enum.KeyCode.D then
        xDelta = xDelta - 1
    end
end)

-- Are you flying?
rs.RenderStepped:connect(function()
    if flying then
        if xDelta ~= 0 or zDelta ~= 0 then
            lastXDelta = xDelta
            local direction = CFrame.new(camera.CFrame.p, (camera.CFrame * CFrame.new(xDelta, 0, zDelta)).p)

            if currentSpeed < (maxSpeed - 5) then
                currentSpeed = currentSpeed + (0.01 * maxSpeed)
            elseif math.floor(currentSpeed) == maxSpeed or currentSpeed > maxSpeed then
                currentSpeed = maxSpeed
            end
            bodyVel.Velocity = direction.LookVector * currentSpeed
        else
            if currentSpeed > 5 then
                bodyVel.Velocity = bodyVel.Velocity / currentSpeed
                currentSpeed = currentSpeed - (.02 * currentSpeed) 
                bodyVel.Velocity = bodyVel.Velocity * currentSpeed
            else
                currentSpeed = 0
            end
        end
    end
end)

My script might be too rigid for what I'm trying to do but I can redo it if I learn the method on how to achieve momentum physics for a more realistic flying.

1 answer

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
5 years ago

Like What You Used, for a more realistic flying experience. Yes, you would want to use Body-velocity. But for continuous momentum, you might want to try and experiment with Body-position.

Ad

Answer this question