Special Laser Sight?

Oh my this has been giving me trouble!

I have this weapon, it doesn’t fire from the center of the screen, but rather from an approximation of the barrel’s location, and the firing direction simulates the sway. I’m having serious trouble trying to make a dot to help the player aim.

I don’t want to show a laser, just a dot at the place the gun would hit. I can’t seem to get it working, and I’m sure my code is fundamentally flawed to boot!

[lua]function DrawBeam() if !CLIENT then return end
local ply = LocalPlayer()
local wep = ply:GetActiveWeapon()

local vm = ply:GetViewModel()

if vm && wep != NULL then
	if wep.SightSpriteSpot == nil then return end
	local attachmentIndex = vm:LookupAttachment("1")
	if attachmentIndex == 0 then attachmentIndex = vm:LookupAttachment("muzzle") end

	local t = {}
		t.start = wep.FirePos
		t.endpos = wep.FirePos + (wep.FireVec*40000)
		t.filter = ply
	local tr = util.TraceLine(t)
	cam.Start3D(Vector(0,0,0), Angle(0,0,0))

– render.SetMaterial(Material(“sprites/bluelaser1”))
– render.DrawBeam(viewModel:GetAttachment(attachmentIndex).Pos, tr.HitPos, 1, 0, 12.5, Color(64, 255, 160, 255))

		local Size = math.Rand(2,3)
		render.DrawQuadEasy(tr.HitPos, (wep.SightSpriteSpot - tr.HitPos):GetNormal(), Size, Size, Color(64,160,255,255), 0)


hook.Add( “HUDPaint”, “DrawBeam”, DrawBeam() )[/lua]

FirePos is the place the gun fires from
FireVec is the vector the bullets shoot
SightSpriteSpot is a calculation made by the function that decides the above two variables

I can not use “self” in that function, since it’s not a weapon function and if I make it a weapon function I don’t know how I’d hook it.

Could you please help me figure this out? My head’s gonna explode!

[editline]28th January 2014[/editline]

I didn’t put any fail safe checks in except to check if the sight location is valid, to avoid checking before the weapon’s initialized?

Not sure why you’re using cam.Start3D, especially with 0,0,0 coordinates. Also not sure why you are drawing 2D in a 3D context.

Try this instead of the cam 3D code:
[lua]render.DrawSphere( tr.HitPos, Size, 3, 3, Color(64,160,255,255) )[/lua]
The 3,3 is the number longitudinal and latitudinal vertices (3,3 should be a diamond, 8,8 would be a close to a sphere)

Looks like you’re trying to draw 3D in HUDPaint, Try changing it to PostDrawOpaqueRenderables

You’re pointing out very valid points. I was thinking I might’ve wanted to use 3D2D

Why a sphere? I want a sprite

[editline]28th January 2014[/editline]

For that matter, I’m having trouble finding the right function to place this in, hook or not. I’m not trying to make a sprite in the center of the screen, and I also don’t want other people to see it, so I feel like that removes some options.

[editline]28th January 2014[/editline]

I want a sprite at the point the gun will actually fire at. All weapons I’d use it in have pretty close to no bullet spread at all (except shotgun(s) and any firing modes that shoot more than one bullet at the same time), but considerable kick, as well as adjustment for the sway of the weapon (when you move, the bob as well), and it’s also affected by viewpunch, making them inaccurate anyway. Yeah, writing the simulating code for those features was a fun walk in the park! The bob/sway are simulated, kick is as kick always was, but the view punch angle is included in the accuracy formula.

Because of all those features I described, the crosshair is a horrible frame of reference, using the eye pos / aim vector variables return pretty much the same problem.

I want it to be smaller by distance as well

If you want to draw a quad like that in HUD Paint, afaik you have to cam.Start3D() then cam.Start3D2D() right after that and then end them accordingly.

From what I understand you’re making the weapons sway and shoot from the gun, rather than the screen and using spread?

I was using a sphere because it was a 3D point, and I didn’t know what you meant.

Any clientside code will only be seen by that client, so you just have to put the function somewhere on the clientside in the PostDrawOpaqueRenderables. Like awcmon said, if you want to draw 2D then you have to use 3D2D

Alright, but why can’t I just place a sprite entity like you would in Hammer, but without making a new entity?

[editline]29th January 2014[/editline]

And yes, you’re right about the gun. Most of it is simulated, not actual… I tried to use the actual coordinates and it was VERY glitchy, even in single player!
I don’t have to ‘make’ the weapon sway or bob, the engine does that itself? I just have to try and imitate it with server code so I can find what direction the bullets need to fire, should be firing.

[editline]29th January 2014[/editline]

I thought I said I didn’t want anyone else to see it, but I was worried it would run client side on every single player’s machine… I guess not
Server is obvious that everyone sees it, but if an entity is made on the server it can present client-side code to everyone there. This weapon is technically a weapon on the server, no? That’s why I was thinking that