Need help with Lerp jitter

I have a local script where I update the viewmodel cframe to the player’s camera cframe and it’s been working great, however I’ve tried to add inertia using Lerp but it jitters way too much.

WITHOUT LERP:

LOCAL SCRIPT:

local targetCFrame	= camera.CFrame
local currentCFrame	= inventoryData.viewmodel.PrimaryPart.CFrame
local newCFrame		= inertia.ApplyInertia(currentCFrame, targetCFrame, deltaTime)
inventoryData.viewmodel:SetPrimaryPartCFrame(newCFrame)

INERTIA MODULE:

local module = {}

module.inertiaSpeed	= 30

function module.ApplyInertia(currentCFrame : CFrame, targetCFrame : CFrame, deltaTime : number)
	
	local alpha = module.inertiaSpeed * deltaTime
	alpha = math.clamp(alpha, 0, 1)
	
	
	local lerpedPosition = currentCFrame.Position:Lerp(targetCFrame.Position, alpha)
	local finalCF = CFrame.new(lerpedPosition) * targetCFrame.Rotation
	
	return finalCF
end

return module

I’ve tried to fix it using AI and searching the forums, thanks in advance.

When do you call ApplyInertia? It seems like you’ve got it delayed until after the render completes, so it ends up applying to the next frame. This looks very much like a 1-frame-late jitter.

The “local script” code is ran using RunService.RenderStepped and I’m pretty sure I tried setting the inertia to return targetCFrame, so bypassing Lerp and I believe it was smooth, I am not home right now so I can’t test again.

Yeah after a bit of testing it seems the :Lerp function is the problem. More accurately whenever the alpha is below 1, aka when the lerp function is actually doing something, is when the jitter occurs. I really don’t know why tho.

Edit:
I tried lerping x, y and z seperately with a general lerp function and the jitter persists, which means CFrame:Lerp wasn’t the issue.
Maybe my character controller is laggy which makes the smooth lerp look jittery, gonna lookinto that now.

Please show us what your loop looks like. This doesn’t show us if it’s run service or something else. Thank you!

The loop is a bit messy also I did say it’s in a RunService.RenderStepped loop above

RunService.RenderStepped:Connect(function(deltaTime)
	
	sway.UpdateSway()
	bob.UpdateBob(deltaTime)
	
	
	if inventoryData.viewmodel then
		
		local bobCFrame = CFrame.new()
		
		if inventoryData.aiming then
			local offset = inventoryData.viewmodel:FindFirstChild(inventoryData.viewmodel.Name).AimPart.CFrame:ToObjectSpace(inventoryData.viewmodel.PrimaryPart.CFrame)
			aimCFrame = aimCFrame:Lerp(offset, .15)
		else
			bobCFrame = bob.bobCFrame
			aimCFrame = aimCFrame:Lerp(CFrame.new(), .1)
		end
		
		
		local targetCFrame	= camera.CFrame * sway.swayCFrame * bobCFrame * aimCFrame
		local currentCFrame	= inventoryData.viewmodel.PrimaryPart.CFrame
		local newCFrame		= inertia.ApplyInertia(currentCFrame, targetCFrame, deltaTime)
		inventoryData.viewmodel:SetPrimaryPartCFrame(newCFrame)
	end
1 Like

okay great so it’s either inertia or it’s related to your CFrame due to a floating point issue. Go into the game and tell me what your CFrame is like where are you in the game when this issue is occurring

1 Like

The map is a baseplate basically, so near 0, 0, 0.
After testing I believe the cause is the player character’s movement being jittery, which I’m not sure why it is like that, I use a custom character controller which is a ball controlled with AssemblyLinearVelocity.

1 Like

yes that is definitely the issue. A standard viewmodel does not use a ball controlled with anything. If you get rid of that mechanism and use a standard viewmodel system you won’t have that issue

1 Like

It seems the base roblox controller has this issue too.

However I plan on using my custom controller anyways.
I think I will look into trying to make inertia based on character movement and not lerp.

1 Like

I respect your passion and I wish you the best of luck with it

1 Like