What are the functions needed to make working ironsights?

Right now I’m working on my SWEP ironsights but I can’t figure out how to make the viewmodel change position smoothly, it will either change exactly at the next frame or with the use of timers make very sharp moves. How do I make smooth ironsights zoom/unzoom and what is the best method to do it(I already have ironsights position setted)?

  1. No timer library
  2. You will need to use maths ( I know it’s hard )
  3. Look at already existing solutions

Alright, got it working as I imagined :

[lua]
function SWEP:SecondaryAttack()

local GetIron = self:GetNWBool( "IronSights" .. self:EntIndex() )
local GetAnim = self:GetNWBool( "IronSightsAnim" .. self:EntIndex() )

if ( !GetIron and GetAnim ) then

	self:SetNWBool( "IronSights" .. self:EntIndex(), true )
	self:SetNWBool( "IronSightsAnim" .. self:EntIndex(), false )
	
	if SERVER then
		self.Owner:SetFOV( 45, 0.3 )
	end
	
	timer.Create( "IronSights" .. self:EntIndex(), self.IronSightsTime, 0, function()
		self:SetNWBool( "IronSightsAnim" .. self:EntIndex(), true )
	end	)
	
end
	
if ( GetIron and GetAnim ) then
	
	self:SetNWBool( "IronSights" .. self:EntIndex(), false )
	self:SetNWBool( "IronSightsAnim" .. self:EntIndex(), false )
	
	if SERVER then
		self.Owner:SetFOV( 0, 0.3 )
	end
	
	timer.Create( "IronSights" .. self:EntIndex(), self.IronSightsTime, 0, function()
		self:SetNWBool( "IronSightsAnim" .. self:EntIndex(), true )
	end	)
	
end

end

function SWEP:GetViewModelPosition( pos, ang )

if ( !self.IronSightsPos ) then return pos end
if ( !self.IronSightsAng ) then return ang end

local GetIron = self:GetNWBool( "IronSights" .. self:EntIndex() )

if ( GetIron != self.LastIron ) then

	self.LastIron = GetIron
	self.IronTime = CurTime()
	
	if ( GetIron ) then 
		self.SwayScale 	= 0.3
		self.BobScale 	= 0.1
	else 
		self.SwayScale 	= 1.0
		self.BobScale 	= 1.0
	end

end

local IronTime = self.IronTime or 0

if ( !GetIron and IronTime < CurTime() - self.IronSightsTime ) then 
	return pos, ang 
end

local Mul = 1.0

if ( IronTime > CurTime() - self.IronSightsTime ) then

	Mul = math.Clamp( (CurTime() - IronTime) / self.IronSightsTime, 0, 1 )
	
	if ( !GetIron ) then Mul = 1 - Mul end

end

local Offset = self.IronSightsPos

if ( self.IronSightsAng ) then

	ang = ang * 1
	ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
	ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
	ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )


end

local Right 	= ang:Right()
local Up 		= ang:Up()
local Forward 	= ang:Forward()



pos = pos + Offset.x * Right * Mul
pos = pos + Offset.y * Forward * Mul
pos = pos + Offset.z * Up * Mul

return pos, ang

end
[/lua]