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

How do you keep velocity in one direction that slowly decreases with opposite force?

Asked by
stepatron 103
5 years ago
Edited 5 years ago

Please include the code which you are trying to use, so the community will be better-equipped to help you with your problem.

Say you're falling with a jetpack, you activate it but it only slows your fall at first because the speed is stronger than the force of the jetpack. But eventually, the speed slows down enough that the jetpack moves you up. How can you achieve such an effect in Roblox if I'm trying to create a jetpack? I'm trying bodyvelocity with a flyscript, but I haven't figured out a way to do it.

My current code:

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

The closest thing I've found is emperormicah's answer in this post but I don't quite understand it

Answer this question