Ad
Log in to vote
0

How do I correctly use Delta Time?

Asked by 3 days ago

I'm working on an FPS game and one of the big problems is uncapped FPS for I use RenderStepped for pretty much everything in the Framework as well as other things. I realize this is bad practice for those such as myself wanting to play the game in a higher FPS but after reading a lot and a lot about Delta Time I just can't understand it, here's a code that I tried but it does nothing and I really just don't know what to do.

How it looks in 60 FPS

How it looks in 240 FPS

run.RenderStepped:Connect(function(deltaTime)

    local originalDeltaTime = deltaTime
    while deltaTime < 1/60+1/((1/originalDeltaTime)+4) do
        deltaTime = deltaTime + game:getService("RunService").RenderStepped:Wait()
        secrecoil()
    end
end)


function secrecoil()

    if cam:FindFirstChild(secondary.Name) then

        if secondary[secondary.Name].Flame.ParticleEmitter.Enabled == true then

            local random = math.random(2)

            if random == 1 then

        for i = 0,s.recup, .1 do

            camcf = camcf * s.recoil
            run.RenderStepped:Wait()

        end

        for i = 0,s.recdown, .1 do

            camcf = camcf * s.recoil2
                    run.RenderStepped:Wait()


        end

        elseif random == 2 then

        for i = 0,s.recup, .1 do

            camcf = camcf * s.recoil3
                    run.RenderStepped:Wait()

        end

        for i = 0,s.recdown, .1 do

            camcf = camcf * s.recoil4
                    run.RenderStepped:Wait()


        end

            end
        end
    end

end

Anything would be helpful at this point. I wanna future proof this game and this is one of the big steps I gotta take.

1 answer

Log in to vote
0
Answered by 3 days ago

The usual way of making things framerate independant is to scale your movement by deltaTime. For example:

local speed = 20
local part = workspace.Part

RunService.RenderStepped:Connect(function(deltaTime)
    -- Multiplying by deltaTime here makes the movement framerate invariant 
    part.CFrame += part.CFrame.LookVector * speed * deltaTime
end)

If you just wanted to run a function roughly n times every second, then you could do it like this:

local elapsedTime = 0
local rate = 1 / 30 -- 30 times a second

RunService.RenderStepped:Connect(function(deltaTime)
    elapsedTime += deltaTime
    if elapsedTime >= rate then
        -- Run function here

        elapsedTime -= rate
    end
end)
0
Even after using this function it still plays at the same speed no matter what the framerate is, the code still works just not as intended. GameBoyOtaku 63 — 3d
Ad

Answer this question