Lerping a gun model to follow the camera is glitching

when I change the lerp number from 1 to anything below it like 0.8, the gun starts to be glitchy and it slow falls down

I’m tryna make it so the gun model follows the camera but also with a lerp so the gun model isn’t so stiff when it moves around

connection1 = runService.RenderStepped:Connect(function()
	local cf = CFrame.new(leftAndRight, height, frontAndBack)
	newGun:PivotTo(newGun.PrimaryPart.CFrame:Lerp(currentCamera.CFrame * cf, 1))
end)

Lerping inside RenderStepped seems excessive seeing as RenderStepped runs at the rate of your FPS, thus usually 60 times per second. Lerp should be used inside lower rate loops, to reduce the calculations while maintaining smoothness.

You also need the “lerp number” (the alpha) to be incremented inside the loop. I’m not entirely sure why the gun keeps falling but I’d assume it’s because of this or your model is unanchored. The alpha is the percentage between your current CFrame and the goal CFrame. Meaning your alpha number of 1 would just be the end result (you lerp does nothing), and 0.8 is the 80% between the current and end position.

You can increment the alpha number like so:

local Camera = game:GetService("Workspace").CurrentCamera

local alpha = 0
local rate = 0.1

while true do
alpha = (alpha + rate) % 1
newGun:PivotTo(newGun.PrimaryPart.CFrame:Lerp(Camera.CFrame, alpha))
task.wait(rate)
end

I tried the code you gave me but when I test it, the gun starts flickering every second

also other question, should i do the actual shooting the gun in a while loop or keep using runservice?

I’m assuming you just want to lock the model to the Camera. Lerping would be pointless, it will be smooth anyway because you are using RenderStepped. If you want the model to sway I would suggest using Springs to tween the CFrame.

RunService.RenderStepped:Connect(function ()
	local cf = CFrame.new(leftAndRight, height, frontAndBack)
	newGun:PivotTo(Camera.CFrame * cf)
end)

ohh okay I’m gonna check out the springs, I never heard of it, but after looking it up, it seems like its what I’m looking for, is to have smoothness to the gun and make it look like your actually moving the gun around.

thanks for the help!

Check out this module, Nevermore has some great modules you can use including the Spring Module.
https://github.com/Quenty/NevermoreEngine/tree/main/src/spring

oh thanks, that’s just what I need and what I was trying to find.

where is the spring module itself? I don’t think it gives me it

Here use this instead its much easier to add to your game if you aren’t using Rojo.
https://devforum.roblox.com/t/physics-based-spring-module/1287742