Laser pointer
From GMod Wiki
Scripted Weapons: Laser Pointer
This simple tutorial will show you how to make a decent laser sight which can be used in your gamemode. It will be toggled by the player's flashlight bind. Note that this has only been used in my gamemode, so i'm not sure how to make all this included in a single SWEP.
All of this is done client-side (for example, in cl_init.lua).
Firstly, you have to define what guns are allowed to draw laser sights. You don't want people using lasers if they're holding a crowbar, right?
AcceptedLaserWeps = { "FN P90", "Nighthawk .50C", "Glock 20", "Super Duper Shotgun 9000" }
This table should contain the full, exact print-name of all the weapons that you want to enable laser sights for. Add as many guns as you want.
Next, make it so that the flashlight bind enables or disables the laser:
function GM:PlayerBindPress( ply, bind, pressed ) if not pressed then return false end //Return if they're releasing the key or else it gets called twice. local ply = LocalPlayer() local togglevis = ply:GetVar("togglevis", "off") if bind == "impulse 100" then if togglevis == "off" then //Turn the laser on! ply:SetVar("togglevis", "on") ply:EmitSound(Sound("items/nvg_on.wav"),100,160) //A nice sound. return true //This disables the flashlight. else ply:SetVar("togglevis", "off") ply:EmitSound(Sound("items/nvg_off.wav")) return true end end end
In a nutshell, this basically sets the player's "togglevis" var to "on" or "off".
Next, we'll get a hook to draw the laser when "togglevis" is set to "on":
function GM:HUDPaint() local ply = LocalPlayer() if (ply:GetVar("togglevis", "off") == "on") then //If his laser is turned on, then do the following: local vm = ply:GetViewModel() if vm and ply:GetActiveWeapon() != NULL and table.HasValue(AcceptedLaserWeps, ply:GetActiveWeapon():GetPrintName()) then local attachmentIndex = vm:LookupAttachment("1") if attachmentIndex == 0 then attachmentIndex = vm:LookupAttachment("muzzle") end //CS:S guns use different attachment names. local t = util.GetPlayerTrace(ply) local tr = util.TraceLine(t) cam.Start3D(EyePos(), EyeAngles()) //Draw the laser beam. render.SetMaterial(Material("sprites/bluelaser1")) render.DrawBeam(viewModel:GetAttachment(attachmentIndex).Pos, tr.HitPos, 2, 0, 12.5, Color(255, 0, 0, 255)) //Draw a laser dot at the end of the beam. local Size = math.random() * 1.35 //That .65 makes all the difference render.SetMaterial(Material("Sprites/light_glow02_add_noz")) render.DrawQuadEasy(tr.HitPos, (EyePos() - tr.HitPos):GetNormal(), Size, Size, Color(255,0,0,255), 0) cam.End3D() end end end
That chunk of code draws the laser if the player has his "togglevis" variable set to "on".
And that's basically it. If you've done it properly, you should have working lasersights when you press your flashlight bind while holding a laser-enabled weapon.
As you can see, the laser doesn't get drawn exactly where it should. That's the fault of the attachments. You'll find that it looks a lot nicer with certain viewmodels (it fits perfectly on the M3 shotgun).