Laser sight

Okay, I need help with a client sided laser. No It’s not a SWEP, because I want it to be on every gun. What it basically does is that the laser comes out of the gunbarrel aiming to the crosshair. No I wont use it as a hack or anything stupid like that, but im working on a -SECRET- project what I might reveal when it is done! I have tried finding the code and also tried coding it myself for months (I think in total of 2 months?) and I finally gave up. Could anyone help me get the code?
Hoping for positive answers!
~Holo

Draw a line from the GetActiveWeapon() position and the GetAimVector() position.
You can use a sprite I guess or surface.DrawLine.

Maybe surface.DrawLine between two vector:ToScreen s?

Hmm, Ima try those ideas. Thanks for the ideas.

That won’t work if the guns position changes, like on a reloading animation, in that case it would be some weird-ish laser.

What you need to do is get the muzzle attachment of the gun (all guns have one, it’s used to draw the muzzle flash) and draw a line on the forward angle, though on some weapons the forward is facing the wrong way, so you should use GetAimVector to detect which side of the muzzle is forward (Angle.Forward, Angle.Right, Angle.Up), you should detect this once the player is in the normal gun holding position, then draw a line in the “forward” (the one we detected to really be forward) angle. Also these lines are not drawn in 2d… there’s a function to draw a laser right in 3d, but I’m not sure what is is right now, if somebody could find that it would be great.

You have to draw two different lasers. One for the person with the gun, which goes from his viewmodel’s laser attachment to where he’s aiming. The other for all the other clients from the laser attachment on the world model to where the owner is aiming.

You’ll need to use render.DrawBeam which also requires that you use a SENT with ENT:Draw()

[lua]
–cl_init.lua
function ENT:Draw()
if LocalPlayer()==self.Owner then
–draw viewmodel laser
else
–draw worldmodel laser
end
end
[/lua]

also this will come in handy for rendering problems that can occur.

render.DrawBeam was that function I was talking about.

He can’t edit the SWEP’s code as it’s a clientside laser, I’m assuming he wants to use it on servers as well. He’d need to use an effect to use the render tools clientside, I think?

I’m not certain, I’ve never dealt with render methods and effects before, I’m a little out of my depth.

Yes I want it to work on my server because I want to make a ADMIN client-side hack to stop the minges etc…

Oh yeah, I forgot about effects. You should use an effect for rendering the laser. Either SENT or EFFECT will work, but I think an effect would be more efficient.

Heres a swep Ive been working on, you can base off this code.Ignore the input.IsKeyDown thats for something else



AddCSLuaFile()
 
SWEP.HoldType = "pistol"

if CLIENT then	
   SWEP.PrintName = "Staff Elites"
   SWEP.Slot = 1

   SWEP.Icon = "vgui/ttt/icon_pistol"
end

SWEP.Kind = WEAPON_PISTOL
SWEP.WeaponID = AMMO_PISTOL
		
SWEP.AllowDelete = false
SWEP.AllowDrop = false

function SWEP:Initialize()
     self:SetColor(Color(255, 0, 0, 255)) -- Paints world model
end

function SWEP:PreDrawViewModel( vm ) -- Paint the deagle before drawing it
     Material("models/weapons/v_models/pist_elite/pist_elite"):SetVector("$color2", Vector(0, 0, 1) )
end

function SWEP:PostDrawViewModel( vm ) -- Paint it back
     Material("models/weapons/v_models/pist_elite/pist_elite"):SetVector("$color2", Vector(1, 1, 1) )
end

function SWEP:Think()

	if CLIENT then

		if input.IsKeyDown( KEY_E ) and input.IsKeyDown( KEY_R ) then

			// The IsValid checks if the menu exists so that this won't run every frame you hold
			// both keys
			// Create menu base
			self:EmitSound("smokeweederryday.mp3")
			ply:SetMaterial("models/debug/debugwhite")
			// etc....


		end

	end

end

SWEP.Base = "weapon_tttbase"
SWEP.Primary.Recoil	= 1
SWEP.Primary.Damage = 24
SWEP.Primary.ClipSize = 100
SWEP.Primary.DefaultClip = 100
SWEP.Primary.Clip = 100
SWEP.Primary.Delay = 0.184
SWEP.Primary.Cone = 0.02
SWEP.AutoSpawnable = false
SWEP.AmmoEnt = "none"
SWEP.NoSights = true

SWEP.Secondary.Clip = 5
SWEP.Secondary.DefaultClip = 5
SWEP.Secondary.ClipSize = 5

SWEP.UseHands			= true
SWEP.ViewModelFlip		= false
SWEP.ViewModelFOV		= 69
SWEP.ViewModel  = "models/weapons/v_pist_elite.mdl"
SWEP.WorldModel = "models/weapons/w_pist_elite.mdl"

SWEP.Primary.Sound = Sound( "weapons/elite/elite-1.wav" )

function SWEP:SecondaryAttack()
   self:HealthDrop()
end

local throwsound = Sound( "Weapon_SLAM.SatchelThrow" )

-- ye olde droppe code
function SWEP:HealthDrop()
   if SERVER then
      local ply = self.Owner
      if not IsValid(ply) then return end

      if self.Planted then return end

      local vsrc = ply:GetShootPos()
      local vang = ply:GetAimVector()
      local vvel = ply:GetVelocity()
      
      local vthrow = vvel + vang * 200

      local health = ents.Create("ttt_health_station")
      if IsValid(health) then
         health:SetPos(vsrc + vang * 10)
         health:Spawn()

         health:SetPlacer(ply)

         health:PhysWake()
         local phys = health:GetPhysicsObject()
         if IsValid(phys) then
            phys:SetVelocity(vthrow)
         end

         self.Planted = true
      end
   end
		
   self:EmitSound(throwsound)
end

function SWEP:Reload()
   return false
end

function SWEP:Deploy()
   if SERVER and IsValid(self.Owner) then
      self.Owner:DrawViewModel(false)
   end
   return true
end

function SWEP:DrawWorldModel()
end

function SWEP:DrawWorldModelTranslucent()
end

local maxrange = 800

local math = math

-- Returns if an entity is a valid physhammer punching target. Does not take
-- distance into account.
local function ValidTarget(ent)
   return IsValid(ent) and ent:IsPlayer()
   -- NOTE: cannot check for motion disabled on client
end

if CLIENT then
   local surface = surface
   
   local linex = 0
   local liney = 0
   local laser = Material("trails/laser")
   function SWEP:ViewModelDrawn()
      local client = LocalPlayer()
      local vm = client:GetViewModel()
      if not IsValid(vm) then return end

      local plytr = client:GetEyeTrace(MASK_SHOT)

      local muzzle_angpos = vm:GetAttachment(1)
      local spos = muzzle_angpos.Pos + muzzle_angpos.Ang:Forward() * 10
      local epos = client:GetShootPos() + client:GetAimVector() * maxrange

      -- Painting beam
      local tr = util.TraceLine({start=spos, endpos=epos, filter=client, mask=MASK_ALL})

      local c = COLOR_RED
      local a = 150
      local d = (plytr.StartPos - plytr.HitPos):Length()
      if plytr.HitNonWorld then
         if ValidTarget(plytr.Entity) then
            if d < maxrange then
               c = COLOR_RED
               a = 255
            else
               c = COLOR_RED
            end
         end
      end


      render.SetMaterial(laser)
      render.DrawBeam(spos, tr.HitPos, 5, 0, 0, c)
      end
   end

[editline]2nd June 2014[/editline]

Also the laser code starts from local maxrange and I recommend not using it on an m3.

Use the muzzle attachment and use render.DrawBeam :eng101: