Laggy spring calculation in a render bind step

Hey everyone, I am currently working on a first-person game. To add a bit more “realism,” I’ve added a bobbing and swaying effect using the Spring module by @Quenty. However, whenever I jump, my micro profiler shows huge lag spikes like the example below:

I first tried to separate the calculations by changing their priority, but nothing worked. I researched debugging, but I didn’t find a solution. So, I am here to ask what could be causing these lags. Here is a part of the controller (Client) that handles the “rendering” of the calculations:

function Local.GetBobbing(Addition: number)
    return math.sin(tick() * Addition * 1.3) * 0.5
end


 local springs = {
        bobbing = Spring.new(),
        swaying = Spring.new()
    }

    local calculatedBobbing = Vector3.new()
    local calculatedVelocity = 0
    

    RunService:BindToRenderStep("HeavyCalculations", Enum.RenderPriority.First.Value, function(deltaTime)
        calculatedBobbing = Vector3.new(Local.GetBobbing(10), Local.GetBobbing(5), Local.GetBobbing(5))
        calculatedVelocity = HumanoidRootPart.Velocity.Magnitude
    end)


    RunService:BindToRenderStep("SpringUpdates", Enum.RenderPriority.Input.Value, function(deltaTime)
        springs.bobbing:shove(calculatedBobbing / 25 * calculatedVelocity / 20)
    end)

    RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function(deltaTime)
        local UpdateBobbleSpring = springs.bobbing:update(deltaTime)
        CurrentCamera.CFrame *= CFrame.Angles(math.rad(UpdateBobbleSpring.X), math.rad(UpdateBobbleSpring.Y), math.rad(UpdateBobbleSpring.Z))
        Humanoid.CameraOffset = self.Constants.Match_Camera_Offset
    end)

    Local._bin:Add(function()
        RunService:UnbindFromRenderStep("HeavyCalculations")
        RunService:UnbindFromRenderStep("SpringUpdates")
        RunService:UnbindFromRenderStep("CameraUpdate")
    end)

Here is just a part of the Controller.
PS: _bin is simply Janitor for disconnecting the events.

Thank you for your help.

1 Like

I helped someone with a similar issue recently, the fix was to delete the AnimSaves in the model, but of course that’s only if you have AnimSaves in the model.

2 Likes

Oh yes, that was it! I ran two tests and yes, that was indeed the issue. Thank you so much!

For those interested, here are the tests:
without the anim saves:

with:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.