Fps bobbing/snapping problem

Hello! This post is a continuation of my previous post that was about bobbing a ViewModel when a player moved. I found out a way to create bobbing for a ViewModel, hooray! But another problem arose from this, which is the snapping.

I have an “if else” statement that runs every frame. If you are moving, do the bobbing on the ViewModel. Else, snap the ViewModel to the Camera’s CFrame. The problem is the snapping.

local function update()
    local t = tick()

    if humanoid.MoveDirection.Magnitude > 0 then
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local cf = CFrame.new(x, y, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
        ViewModel.Head.CFrame = Camera.CFrame


The problem is that when the player stops moving, the bobbing stops and moves the ViewModel instantly to the player’s Camera, vice-versa. The result is a ViewModel that looks like it snaps to the Camera when still. I want the ViewModel to visibly return to the player’s Camera. I have tried Lerping but the effect is off and not what I am looking for.

Any tips would help.

Have you tried using a tween? you are instantly setting it so that’s why it’s instant

Try incorporating the step parameter of RenderStepped. If done correctly this will make so the code “adapts” to the diferent runtimes of renderstepped. I’d recomend checking this https://developer.roblox.com/en-us/api-reference/event/RunService/RenderStepped, since it has a good example of a code using the step parameter.