Viewmodel lags behind when using :lerp()

So I’m trying to find a way to make my viewmodel’s movement more smooth, and not just instant snap to the camera. I’d like to use lerp to do this, but I have a problem, the viewmodel delays behind the camera.

Other people have had the problem, and posted about it, but I can’t find a straight answer that I can understand.

My code (in a RenderStepped function):

if loadout.current.PrimaryPart ~= nil then
	loadout.current.HumanoidRootPart.CFrame = loadout.current.HumanoidRootPart.CFrame:lerp(thing,0.2) 
end

Loadout.current is the gun
“thing” is the camera CFrame multiplied by walking offsets, recoil offsets, etc. (these aren’t the issues)

Video:

I’ve dug into this developer hub article, but to no avail. I think what I might be looking for is ignoring the XYZ values and using the camera’s, but not the orientation values.

2 Likes

You can overwrite the position part of the CFrame like this:

local hrp = loadout.current.HumanoidRootPart
local newCFrame = hrp.CFrame:lerp(thing, 0.2) 
newCFrame = newCFrame - newCFrame.p + hrp.CFrame.p
hrp .CFrame = newCFrame

Now I just see the viewmodel at the middle of the map.
image

Also I forgot to give context on what “thing” is, it’s basically just the camera CFrame multiplied by recoil offsets, walking offsets, aiming offsets, etc, and Loadout.current is the viewmodel

Is that not what you wanted? You used Lerp, and now it’s moving slower, and not instantly snapping. I guess you could try increasing the alpha value from 0.2 to 0.5 or higher.

1 Like

Lerp makes it smooth-looking. The problem is that it delays behind, I only want the rotation to be delayed, not the position.

there is so much ways to say this

1 Like

Have you tried messing about with the priority of the render stepped?

1 Like

Actually Lerp doesn’t make it smooth looking for the position. You’re delaying the position too.

How do I not do that though?

Well by initially setting the position or the CFrame without the rotation then lerping the CFrame.

If I try doing that it says “Position cannot be assigned to”.

I’ve just realized something- directly setting the position and orientation of the root part doesn’t apply to the welds/motor6ds.

This is my code currently:

				local function CFrameToOrientation(cf)
					local x, y, z = cf:ToOrientation()
					return Vector3.new(math.deg(x), math.deg(y), math.deg(z))
				end
				local lthing = loadout.current.HumanoidRootPart.Orientation:lerp(CFrameToOrientation(thing),0.2)
				loadout.current.HumanoidRootPart.Position = workspace.CurrentCamera.CFrame.Position
				loadout.current.HumanoidRootPart.Orientation = lthing

If the humanoidrootpart is selected in game, you can see it locked onto the camera with the correct orientation and such, but the arms are not following. I think if I were to combine the orientation and position and make it into a CFrame somehow, and use :SetPrimaryPartCFrame(), this will work?

1 Like

That’s why I stated or the CFrame without the orientation.

Assuming you’re using a model you’d have to set the primarypart CFrame.

You can get the CFrame without the orientation by doing CFrame.new(CFrame.Position)

How do I add the orientation into that CFrame?

By multiplying the angles onto the current CFrame after you applied the position CFrame.

local function CFrameToOrientation(cf)
					local x, y, z = cf:ToOrientation()
					return Vector3.new(math.deg(x),math.deg(y),math.deg(z))
				end
				local lthing = loadout.current.HumanoidRootPart.Orientation:lerp(CFrameToOrientation(thing),0.2)
				loadout.current.HumanoidRootPart:SetPrimaryPartCFrame(CFrame.new(workspace.CurrentCamera.CFrame.Position)*CFrame.Angles(math.rad(lthing.X),math.rad(lthing.Y),math.rad(lthing.Z)))
				

Speen

1 Like

I just re edited the code so maybe try that again.

The rotations seem a little weird