CFrame Lerp issues

Hello so I am working on a new FPS style game and I am working on Iron sights. I wanted the gun to have an animation in between the Hip fire and when aiming down the sight. So I used a CFrame Lerp for this. The issue is it seems like it skips the animation completely like this:

Here is the code for the Function that controls the Animation. Everything else in the code should be fine, it is just the animation that needs fixing:

function AimAnim()
	if model ~= nil then
		if aiming == true then
			local CFrame1 = cam.CFrame * CFrame.new(model.CFrameSet.Value)
			local CFrame2 = cam.CFrame * CFrame.new(model.AimCFrameSet.Value)
			
			for i = 0, .2, 1/60 do
			    model.PrimaryPart.CFrame = CFrame1:lerp(CFrame2,i)
			    wait()
			end
			
			aiminganim = false
		else
			local CFrame2 = cam.CFrame * CFrame.new(model.CFrameSet.Value)
			local CFrame1 = cam.CFrame * CFrame.new(model.AimCFrameSet.Value)
			
			for i = 0, .2, 1/60 do
			    model.PrimaryPart.CFrame = CFrame1:lerp(CFrame2,i)
			    wait()
			end
			
			aiminganim = false
		end
	end
end

Thank you!

1 Like

That’s because wait() is very short and it is barely noticeable.

instead of making it 60fps, making it 20fps would still create a relatively smooth effect.


So, instead of doing this:

for i = 0, .2, 1/60 do
    model.PrimaryPart.CFrame = CFrame1:lerp(CFrame2,i)
    wait()
end

Do something like this:

for i = 0, .2, 0.05 do
    model.PrimaryPart.CFrame = CFrame1:Lerp(CFrame2,i)
    wait(0.05)
end
1 Like

Ok, so I gave it a try. Nothing much has changed. However it did increase the time it takes to start aim down sights. There is still no animation in between it. Thanks for the suggestion though!

Here’s another suggestion.

try using TweenService to tween the parts. Unanchor all the parts except for the part that is tweened, and put weld constraints in the tweened part to connect all of the other parts.

And lastly, tween the tweened part and the model should be tweened alongside it.


If you don’t understand what I mean, maybe try looking at this topic.

1 Like

Thanks for suggesting the TweenService for parts now the animation is super smooth. There are a few things I need to tweak like when you want to aim down while moving but this is a great leap in the right direction. Thank you so much:

1 Like