FPS Aim-down-sights wont return to camera CFrame properly

Recently I’ve been trying to make an Aim-Down-Sights system in studio via an AimPart on a ViewModel gun. What I have so far is a ViewModel who’s head’s CFrame is bound to the players camera, and a system that allows you to aim down the sights smoothly.

The problem is that when I try to apply the same code to make the viewmodel return to its regular position, it simply locks into place without smoothing.

What I have right now is code that is trying to lerp the viewmodels head’s CFrame back to the camera CFrame. The problem is that it simply doesn’t want to transition smoothly. I’ve tried inversing the offset used to aim down sights, but that isn’t good either.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

What I have right now:

local ArmModel = workspace:FindFirstChild("Arms"..plr.Name)
		local Weapon = ArmModel:FindFirstChild("M4A1")
		
			
			local lastCameraCF = Camera.CFrame
			local swayOffset = CFrame.new(0,0,0)

				local mult = 3
				local rotation = Camera.CFrame:toObjectSpace(lastCameraCF) --get cframe delta.
				local x,y,z = rotation:ToOrientation() --I'm sure there are better ways to get rotation but this will work for now.
				swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN
			--the offset used to aim down sights
		local aimoffset = ArmModel:FindFirstChild("M4A1").AimPart.CFrame:Inverse() * ArmModel.Head.CFrame
			
		local newcf = Camera.CFrame * aimoffset
		
--lerps the viewmodel cframe to aim down the sights
			for t = 0, 101, 8 do
				game:GetService("RunService").RenderStepped:Wait();
			ArmModel.Head.CFrame = ArmModel.Head.CFrame:Lerp(newcf * CFrame.new(0,0,-0.3), t/100);
			aimoffset = ArmModel:FindFirstChild("M4A1").AimPart.CFrame:Inverse() * ArmModel.Head.CFrame

			 newcf = Camera.CFrame * aimoffset
			end
			Aiming = "Aiming"

Here is the code to stop aiming down sights:

	local ArmModel = workspace:FindFirstChild("Arms"..plr.Name)
		local Weapon = ArmModel:FindFirstChild("M4A1")
		

		local lastCameraCF = Camera.CFrame
		local swayOffset = CFrame.new(0,0,0)

		local mult = 3
		local rotation = Camera.CFrame:toObjectSpace(lastCameraCF) --get cframe delta.
		local x,y,z = rotation:ToOrientation() --I'm sure there are better ways to get rotation but this will work for now.
		swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN

		local aimoffset = ArmModel:FindFirstChild("M4A1").AimPart.CFrame:Inverse() * ArmModel.Head.CFrame
		local aimoff = aimoffset:Inverse()
		

		
		
	for t = 0, 101, -10 do
			game:GetService("RunService").RenderStepped:Wait();
			ArmModel.Head.CFrame = ArmModel.Head.CFrame:Lerp(Camera.CFrame, t/100);
		end
		Aiming = "Not"

For some reason, lerping it back to the Camera’s CFrame doesn’t work.

Footage:
https://gyazo.com/e42be1d6f96b613ae48dca9d14e03942

2 Likes