Tweening Viewmodel Relative to Camera

I’m working on making an item bob back and forth slightly in the players hand while they walk. So far I have something basic that works fairly well, but the problem is the transition: When the player first starts moving or stops moving the object snaps back to the starting /ending position respectively. I believe my solution will be to use tweens so it smoothly glides to those positions first. You will see my attempt at this below:

function viewmodelBob()
    local currentCF = cam.CFrame*CFrame.new(pos,spin)
    local t = tick()
    local humanoid = player.Character:FindFirstChild("Humanoid")
    if humanoid.MoveDirection.Magnitude > 0 then
        local y = math.abs(math.sin(t * 5)) * 0.15
        local z = math.cos(t * 5) * 0.15
        local cf = currentCF * CFrame.new(0, y, z)
        viewmodel.CFrame = cf
    else
        viewmodel.CFrame = cam.CFrame*CFrame.new(pos,spin)
        local goal = {}
        goal.CFrame = cam.CFrame*CFrame.new(pos,spin)
        local tweenInfo = TweenInfo.new(1)
        local tween = TweenService:Create(viewmodel,tweenInfo,goal)
        tween:Play()
    end
    if HitPoint then att1.WorldPosition = HitPoint end
end

Instead of moving the part relative to the camera smoothly and beautifully the viewmodel just gets locked in whatever position it started for the duration of the tween before reverting to normal bobbing back and forth.

3 Likes

i suggest you to use lerp, it’s like using the method for camera bobble but for fps