• Iron Sights for a Swep
    6 replies, posted
  • Avatar of mescool15
  • Just how exactly would I make iron sights for a swep? Searching is disabled by the way so i can't search.
  • Avatar of lexic
  • 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. [url=http://www.garrysmod.org/downloads/?a=view&id=29787][img]http://www.garrysmod.org/img/?t=dll&id=29787[/img][/url] Have fun!
  • Avatar of mescool15
  • I'm getting a constant error and I don't know how to fix it. The iron sights worked though. [IMG]http://i276.photobucket.com/albums/kk35/mescool15/penis.jpg[/IMG]
  • Avatar of MakeR
  • Show us line 201 in shared.lua, my best guess is that self.Primary.Cone isn't defined. Also, penis.jpg ... wtf
  • Avatar of mescool15
  • Hold on, Some asshole uninstalled Notepad ++ off my computer. EDIT: Here it is. local scale = 10 * self.Primary.Cone
  • Avatar of MakeR
  • 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 [url]http://www.cathybarryadultstore.com/[/url]. 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 [B]I[/B] 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! [IMG]http://pic.esmatube.com/imgs/a/g/t/x/x/giant_dildo_in_my_ass_part_ii-3_tmb.jpg[/IMG] [IMG]http://pic.esmatube.com/imgs/a/d/d/h/r/bbw_giant_dildo_squat_25cm_round-3_tmb.jpg[/IMG] [IMG]http://xxxdessert.com/tube/contents/videos_screenshots/15000/15261/preview.mp4.jpg[/IMG] [IMG]http://g04.a.alicdn.com/kf/HTB1JlHlIpXXXXaJXVXXq6xXFXXXo/Silicone-Huge-Dildos-Vibrator-for-woman-shake-Horse-Dildos-penis-swith-sucker-african-Dong-dildos.jpg[/IMG] Sponsored Images: [IMG]http://45.media.tumblr.com/f81f8d61fa253dc804edb423c8ab149f/tumblr_mfbk96QAug1rvbfkuo1_500.gif[/IMG] [IMG]http://49.media.tumblr.com/9110819111aa37acf4cd1c9d5341dd97/tumblr_nc4hqkdESo1skwv6ko1_500.gif[/IMG] [IMG]http://1.bp.blogspot.com/-gueWbSnnsIc/UUtenyLS0oI/AAAAAAAANF4/L4aZnTrMaqs/s1600/dermatologists-hate-her.jpg[/IMG] [IMG]http://static.fjcdn.com/pictures/Gym+leaders+hate+him_ab471d_5017861.jpg[/IMG] [IMG]https://static.fjcdn.com/comments/That+one+with+the+lamp+got+me+_e68beb19422eea14289caa5f4d6dffb7.jpg[/IMG] Got any questions? Visit my Steam Profile! [url]http://steamcommunity.com/id/Robotboy655[/url]