How would you keep a weapon's position relative to the viewmodel's HRP when lerping the gun?

The only part in my viewmodel that is moved is the gun’s handle, the rest of the gun is welded to the handle, and the hands use inverse kinematics for the holding and reloading animations.

Because of this when I lerp the gun for example for aiming and reloading, the gun lags behind, presumably because it’s also lerping the position (which is always kept relative to the viewmodel’s HRP unless aiming).

In the video below, you can see how bad this looks when I spam the aim button, or reload even, as it lags behind a noticeable amount, this is also an issue with reloading and because of this I’ve had to keep the time increase of the lerp alpha to pretty smal numbers so it’s not so noticeable, but this looks bad as it makes gun movements very snappy.

I’ve tried to fix this by separating the viewmodel’s CFrame from the target CFrame for the gun’s handle and multiplying the viewmodel’s cframe by the lerp before setting it to the handle’s CFrame, which only causes the viewmodel to freak out if the lerp alpha is less than 1.

How I set the viewmodel’s CFrame:

vm_hrp.CFrame = cam.CFrame * vmOffset.Value

The code that sets the target Cframe:

gunTargetCFM = (
			not aiming and vm_hrp.CFrame * gunOffset * fireRecoilHip * springPositionalOffset * springAngularOffset or 
				gunHandle.CFrame * getAimOffset() *recoilPositionalOffset*fireRecoilAiming * sineOffset * swayAimOffset * springADS()		
		)

The code that lerps the aim transition:

gunHandle.CFrame = gunHandle.CFrame:Lerp(gunTargetCFM,(aimTime < 1 and aimTime or 1))
-- same as reload transition:
gunHandle.CFrame = gunHandle.CFrame:Lerp(gunTargetCFM,reloadAlpha.Value/100)

I’m honestly not sure how to do this as I know it has to do with the gun’s position lagging behind because the part that keeps it relative is being lerped, however I do need to change the gun’s position and rotation when aiming or reloading while keeping it relative, and my attempts to fix it have only made it worse.

What happens when I add the viewmodel’s cframe to the target lerped cframe :

What this looks like:
(targetCFM is the same as gunTargetCFM)

targetCFM = gunHandle.CFrame:Lerp(gunTargetCFM,(aimTime < 1 and aimTime or 1))
gunHandle.CFrame = not aiming and vm_hrp.CFrame * targetCFM or targetCFM

As you can see, any gun movement when I separate the viewmodel CFrame from the lerp CFrame makes the gun disappear(??), I’ve looked all over devforum for a fix for over a week now, and experimented a few different ways myself but never found anything that actually worked.

You can’t lerp the view model CFrame directly to create these sorts of animations. You want the animation to be in the camera’s object space, but setting the CFrame of a model sets it in world space.

Try something like this:

local viewModelOffset = CFrame.new()
local targetViewModelOffset = CFrame.new()

local RECOVER_SPEED = 5.0

function shoot()
    --Instantaneously change the offset to create a recoil effect
    viewModelOffset *= CFrame.Angles(-0.05, 0, 0)
end

function reload()
    --Gradually move view model down
    targetViewModelOffset = CFrame.new(0, -2, 0)
    wait(1)
    --Gradually move view model back up
    targetViewModelOffset = CFrame.new()
end

function recoilRecover(dt)
    viewModelOffset = viewModelOffset:Lerp(targetViewModelOffset, math.clamp(dt * RECOVER_SPEED, 0, 1))
end

function updateViewModel(dt)
    recoilRecover(dt)
    --Viewmodel is always stuck to camera, so character walking around doesn't cause it to lag behind.
    --This is because the overall CFrame is *not* lerped.
    --Also, view model offset can easily be lerped because it's in the camera's object space
    viewModel:PivotTo(camera.CFrame * viewModelOffset)
end

It’s obviously not a full animation system but it illustrates what I said above

2 Likes

I understand that I have keep the handle in the camera’s worldspace, but how exactly do I do it while maintaining the integrity of the lerp position and rotation?

I’m not pivoting the model, just the handle, I tried to use targetCFM:ToWorldSpace(camera.CFrame) but this led to the lerp moving outside of the bounds, even when setting an offset to make it as close to the camera as possible, moving the camera like this led to the gun rotating with the camera, but not with an offset, just with the camera’s same rotation.

I misunderstood what you said I think as I was able to figure out a way to make it work by using :ToObjectSpace and separating the rotation from the position CFrame and reapplying it in a different way…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.