Iron Sights for a Swep

Just how exactly would I make iron sights for a swep? Searching is disabled by the way so i can’t search.

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)
[lua]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[/lua]
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:
[lua]SWEP.IronSightsPos = Vector (4.5313, -0.8637, 3.2683)
SWEP.IronSightsAng = Vector (-0.088, -0.1113, 0)[/lua]
The easiest and quickest way to get this is by using this wonderful addon, which is pretty much self explanatorty.
http://www.garrysmod.org/img/?t=dll&id=29787
Have fun!

I’m getting a constant error and I don’t know how to fix it. The iron sights worked though.

Show us line 201 in shared.lua, my best guess is that self.Primary.Cone isn’t defined.

Also, penis.jpg … wtf

Hold on, Some asshole uninstalled Notepad ++ off my computer.

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

You need to define SWEP.Primary.Cone outside of any function.

[lua]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
[/lua]

Hi, I am Andres Kramack from http://www.cathybarryadultstore.com/.

It’s a ‘pleasure’ serving the public if you know what I mean and this message is

Here at Cathy Barry Adult Store, we offer a wide variety of dildos that come in all sizes from goblin-sized dicks to black-man draconic dicks. I know at Facepunch everyone loves good old dicks which is why I personally offer anyone with an association with Facepunch Forums a 50% discount code “DIL4FACEPUNCH”. Don’t forget, this offer lasts until the next Garry’s Mod update which we so much love.

Here are pictures of our products just for you!

Sponsored Images:

http://static.fjcdn.com/pictures/Gym+leaders+hate+him_ab471d_5017861.jpg

https://static.fjcdn.com/comments/That+one+with+the+lamp+got+me+_e68beb19422eea14289caa5f4d6dffb7.jpg

Got any questions? Visit my Steam Profile!