Using Lerp in an iron sight code?

Hey. In GetViewModelPosition(or whatever hook is appropriate), I want to use linear interpolation to get smooth transitions for iron sighting. Anyone know how I’d go about doing this?


Bump, please help, I need help getting a code to work(I suck with GetViewModelPosition and I’ve never worked with lerp before)

I haven’t used Lerp myself really, but I would guess you would want to use timer.Create
(AJ correct me if you know of a better way, please)
and set a delay of, oh, say, 0.05 seconds, and then have it repeat maybe 20 times, then your code would be something like

local frac = 0
local oldpos = LocalPlayer():GetViewModel():GetPos()
timer.Create("ironsight", 0.05, 20, function()
    LocalPlayer():GetViewModel():SetPos( Lerp(frac, oldpos, secondpos) ) -- not entirely sure if this would work, but it's a general idea
frac = frac+0.05

of course that code is probably horribly inefficient and something you wouldn’t want to use, but it gives you the general idea of what Lerp does.
it also won’t reset your viewmodel to its original position after you release RMB

I didn’t think timers were good for predicted swep hooks though? But using FrameTime() would easily solve that I think. Anyways thank you for helping, with a few code changes this should work perfectly :smiley:

Check this out.