Aim Down Sight problem (lerping)

its been days since i tried to make my ads alone, but i cant and i found out a video talking about CFrame:Lerp, i tried to use it on my own script but doesnt work:

this is the idle version, everything normal. the cube at the left of the gun is the Sight, the goal

and then, instantly after i press the key to aim, it will reach the goal WITHOUT doing it “smoothly”

this is the code btw, if you have questions i will answer the faster i can.

RunService.RenderStepped:Connect(function(Delta)
	ViewModel:SetPrimaryPartCFrame(camera.CFrame * AimCFrame)
	
	if isAiming then
		local offset = ViewModel.Sight.CFrame:ToObjectSpace(ViewModel.CameraPart.CFrame)
		AimCFrame = offset:Lerp(offset, 0.1)
		print("lerp") --it prints, but alot in a fraction of second
		
	else
		local offset = CFrame.new()
		AimCFrame = offset:Lerp(offset, 0.1)
	end
--i tried adding a task.wait(0.1) but it doesnt affect the code
end)
1 Like

I think you’ve misunderstood how lerping (linear interpolation) works.

The way linear interpolation works is it returns a value between two points, as far along as the given alpha goes. The alpha is just a fancy way of saying how far along you want to go.

Here’s an example:

local function lerp(startV: number, endV: number, alpha: number): number
    return startV + (endV - startV) * alpha
end

--an example, if we want to go half way between 100 and 105
--get the alpha, which is half way between the numbers
local alpha = 1 / 2 --0.5

--lets' run the lerping function
local result = lerp(
    100, --the start value
    105, --the end value
    alpha --the alpha to interpolate with
)

print(result) --this will come out as 102.5

--this is another example, using an iteration.
for i = 1, 100, 1 do
    local alpha = i / 100 --how far along in the iteration out of the final value are we?
    local result = lerp(1, 100, alpha)
    print(result) --your output will gradually go from 1 to 100 over 100 print statements
end

--what you are essentially doing
local result = lerp(1, 1, 0.1)
print(result) --> 1

So, your code, which is this:

is essentially not lerping at all. You say you want to interpolate between the start and end values, which are the same values, so the difference is 0. You then multiply 0 by 0.1, and you get, well, 0, hence why it isn’t smooth.

CFrame:Lerp() works the same way (produces the same result, respectively) as my example lerping function I showed. To get an interpolated CFrame, you need a start CFrame and an end CFrame, and a fractional alpha to interpolate with. Basically, you can’t interpolate with the 2 same CFrames, which is what you are trying to do.

I hope this helps!

1 Like

you are the best, i didnt understood Lerp, but you managed to explain it clear and simple, thank you!

1 Like

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