Viewmodel moves relative to world

I’m not good with these math things like Vectors and CFrames, and i’ve been with this problem for quite a bit of time.

I made a probably bad viewmodel script that sways depending on my character’s velocity, the problem is that it only moves relative to the world. I can’t really explain it in words so here is a video:

Here is the code:

RS.RenderStepped:Connect(function(DT)
	local Fixed = Clamp(RootPart.AssemblyLinearVelocity)

	local VelocityOffset = CFrame.new(-Fixed / (Dec * 10))
	local Offset = LastVelocityOffset:Lerp(VelocityOffset, DT * Sens)
	LastVelocityOffset = Offset
	
	local TargetPosition = Camera.CFrame * CFrame.new(.25, -1.5, -1) * Offset
	Model:PivotTo(TargetPosition)
end)

--> Clamp(RootPart.AssemblyLinearVelocity) is just a cap to how much the viewmodel can sway.

I have absolutely zero idea on how to fix this and i’ve tried a bunch of things.

If the post is lacking information send here and i will provide it if possible.

Hi! To get the position in front of something relative to that object space , you can use pary.CFrame.LookVector

I have already tried using lookVector but it did not work, at least in the way i implemented it.

What’s the purpose of the last Offset variable? In my experience with view models, that’s not needed.

I think you’re talking about the offset i’m calculating before moving the model, right?

Well, that is basically the sway. I’m getting the velocity of the player’s rootPart and then adding it to the model’s offset after reducing it.

The lerp part is just so it doesn’t snap into place and rather move there smoothly.

Without that sway, does it work properly?

Without the sway it just stays in place, which is not the objective, what i want to achieve is like a delay on the movement compared to the camera, so it feels more real. A bit like how inertia works.

Yes, I understand. I was just making sure it would work. I would suggest changing your method of swaying to use a spring module as that has worked very well for me in the past and allows for a lot of customization.

Here is an example below: