1. Post #1
    mescool15's Avatar
    August 2008
    1,069 Posts
    Just how exactly would I make iron sights for a swep? Searching is disabled by the way so i can't search.

  2. Post #2
    Gold Member
    Lexic's Avatar
    March 2009
    6,123 Posts
    You first need the code that does it. You can either inherit from a base (Such as weapon_cs_base which everyone has) or put the code in yourself. (This is a straight rip from weapon_cs_base incidentally)
    local IRONSIGHT_TIME = 0.25
    
    /*---------------------------------------------------------
       Name: GetViewModelPosition
       Desc: Allows you to re-position the view model
    ---------------------------------------------------------*/
    function SWEP:GetViewModelPosition( pos, ang )
    
    	if ( !self.IronSightsPos ) then return pos, ang end
    
    	local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )
    	
    	if ( bIron != self.bLastIron ) then
    	
    		self.bLastIron = bIron 
    		self.fIronTime = CurTime()
    		
    		if ( bIron ) then 
    			self.SwayScale 	= 0.3
    			self.BobScale 	= 0.1
    		else 
    			self.SwayScale 	= 1.0
    			self.BobScale 	= 1.0
    		end
    	
    	end
    	
    	local fIronTime = self.fIronTime or 0
    
    	if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
    		return pos, ang 
    	end
    	
    	local Mul = 1.0
    	
    	if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
    	
    		Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
    		
    		if (!bIron) 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
    
    
    /*---------------------------------------------------------
    	SetIronsights
    ---------------------------------------------------------*/
    function SWEP:SetIronsights( b )
    
    	self.Weapon:SetNetworkedBool( "Ironsights", b )
    
    end
    
    
    SWEP.NextSecondaryAttack = 0
    /*---------------------------------------------------------
    	SecondaryAttack
    ---------------------------------------------------------*/
    function SWEP:SecondaryAttack()
    
    	if ( !self.IronSightsPos ) then return end
    	if ( self.NextSecondaryAttack > CurTime() ) then return end
    	
    	bIronsights = !self.Weapon:GetNetworkedBool( "Ironsights", false )
    	
    	self:SetIronsights( bIronsights )
    	
    	self.NextSecondaryAttack = CurTime() + 0.3
    	
    end
    
    /*---------------------------------------------------------
    	DrawHUD
    	
    	Just a rough mock up showing how to draw your own crosshair.
    	
    ---------------------------------------------------------*/
    function SWEP:DrawHUD()
    
    	// No crosshair when ironsights is on
    	if ( self.Weapon:GetNetworkedBool( "Ironsights" ) ) then return end
    
    	local x = ScrW() / 2.0
    	local y = ScrH() / 2.0
    	local scale = 10 * self.Primary.Cone
    	
    	// Scale the size of the crosshair according to how long ago we fired our weapon
    	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
    	scale = scale * (2 - math.Clamp( (CurTime() - LastShootTime) * 5, 0.0, 1.0 ))
    	
    	surface.SetDrawColor( 0, 255, 0, 255 )
    	
    	// Draw an awesome crosshair
    	local gap = 40 * scale
    	local length = gap + 20 * scale
    	surface.DrawLine( x - length, y, x - gap, y )
    	surface.DrawLine( x + length, y, x + gap, y )
    	surface.DrawLine( x, y - length, x, y - gap )
    	surface.DrawLine( x, y + length, x, y + gap )
    
    end
    
    /*---------------------------------------------------------
    	onRestore
    	Loaded a saved game (or changelevel)
    ---------------------------------------------------------*/
    function SWEP:OnRestore()
    
    	self.NextSecondaryAttack = 0
    	self:SetIronsights( false )
    	
    end
    After you have somehow got that in your swep, you need to define your ironsights data. This is different between each weapon. For instance for the model 'models/weapons/v_pist_fiveseven.mdl' you would use this:
    SWEP.IronSightsPos = Vector (4.5313, -0.8637, 3.2683)
    SWEP.IronSightsAng = Vector (-0.088, -0.1113, 0)
    The easiest and quickest way to get this is by using this wonderful addon, which is pretty much self explanatorty.

    Have fun!

  3. Post #3
    mescool15's Avatar
    August 2008
    1,069 Posts
    I'm getting a constant error and I don't know how to fix it. The iron sights worked though.


  4. Post #4
    Taught by John Lua
    MakeR's Avatar
    May 2007
    2,914 Posts
    Show us line 201 in shared.lua, my best guess is that self.Primary.Cone isn't defined.

    Also, penis.jpg ... wtf

  5. Post #5
    mescool15's Avatar
    August 2008
    1,069 Posts
    Hold on, Some asshole uninstalled Notepad ++ off my computer.

    EDIT: Here it is. local scale = 10 * self.Primary.Cone

  6. Post #6
    Taught by John Lua
    MakeR's Avatar
    May 2007
    2,914 Posts
    You need to define SWEP.Primary.Cone outside of any function.

    SWEP.Primary.Cone = 0.02 --This is how accurate your gun is, a cone of 0 is completely accurate, a cone of 1 is horribly accurate