Laser pointer

From GMod Wiki

Jump to: navigation, search

Scripted Weapons: Laser Pointer

Yarin Kaul Icon Lua48.png
Description:Make a decent laser pointer on your SWEP.
Original Author(s):Rambo_6 and Cringerpants

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.
                        ply:SetVar("togglevis", "off")
                        return true

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.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.DrawQuadEasy(tr.HitPos, (EyePos() - tr.HitPos):GetNormal(), Size, Size, Color(255,0,0,255), 0)

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).

Personal tools
Lua Scripting