Custom HUD problem

Currently working on a custom HUD where I want to make a “fake” 3D HUD where the health and ammo bar jiggle about as you look around. The subtle movement part works fine but all the HUD assets like to slowly drift up and to the left until they reach 0,0 on my screen.



local posx = ScrW() / 18.824 -- OCD will be the death of me
local posy = ScrH() / 1.06 

hook.Add("HUDPaint" , "HealthHud" , function()

// make HUD jiggle a bit

local CurAng = EyeAngles()

local clampX,clampY = 10 , 10

posx = math.Approach(posx,math.Clamp(CurAng.x, -clampX, clampX), FrameTime()*65)
posy = math.Approach(posy,math.Clamp(CurAng.y, -clampY, clampY), FrameTime()*65)



originally I tried to use math.Clamp on posx and posy in math.Approach and this just locked all of my HUD assets to 0,0 completely ignoring the original posx and posy.

I’m just getting into Lua so I apologize if problems like this are tedious and stupid, any bit of help would be appreciated.

bump

It’s literally been five hours… Usually bumps are done around every 24 hrs.

Oops?

I don’t know what the problem is, but based on the wiki page for

FrameTime… shouldn’t you use

RealFrameTime?

I realized that shortly after I posted, thanks for the tip though.

Tried using math.min/math.max instead of math.clamp but that didn’t help either

Where do you actually draw the HUD?

He has it drawing in a “HUDPaint” hook

facepalm
The actual line that draws the HUD, I would like to see it.

here’s pretty much everything, other than some font stuff and hiding the default HUD which was copied directly from the wiki (I know my ScrH/ScrW shit is kinda hacky, pls no roast).

Edit moved down below

The HUD draws fine and the HUD moves around within the math.Clamp properly, but the HUD elements are stuck at 0,0 on my screen if I apply the clamp to posx and posy, and if it’s applied to CurAng the HUD slowly drifts away from the proper position until it reaches 0,0

It doesn’t matter what line does that, the problem isn’t drawing the HUD, it’s the X and Y coordinates being incorrect. If you want to know what it looks like, then just draw a rectangle:



local posx = ScrW() / 18.824 -- OCD will be the death of me
local posy = ScrH() / 1.06 

hook.Add( "HUDPaint", "HealthHud", function()

// make HUD jiggle a bit

local CurAng = EyeAngles()

local clampX,clampY = 10 , 10

posx = math.Approach(posx,math.Clamp(CurAng.x, -clampX, clampX), FrameTime()*65)
posy = math.Approach(posy,math.Clamp(CurAng.y, -clampY, clampY), FrameTime()*65)

surface.SetDrawColor( 255, 255, 255 )
surface.DrawRect( posx, posy, 100, 100 )

end )


[editline]30th October 2017[/editline]

ninja’d

Still haven’t found a solution, here’s the full code if anyone wants to see for themselves.



local hide = {
	CHudHealth = true,
	CHudBattery = true,
	CHudAmmo = true,
	CHudSecondaryAmmo = true
}

hook.Add( "HUDShouldDraw", "HideHUD", function( name )
	if ( hide[ name ] ) then return false end
end )



surface.CreateFont("HUDFont", {
	font = "Arial",
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0, 
	antialias = true,
	underline = false, 
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

local posx = ScrW() / 18.824 -- OCD will be the death of me
local posy = ScrH() / 1.06 

hook.Add("HUDPaint" , "HealthHud" , function()
smoothHealth = Lerp(10 * FrameTime() , smoothHealth , LocalPlayer():Health())

// make HUD jiggle a bit

local CurAng = EyeAngles()
local clampX,clampY = 20 , 20

posx = math.Approach(posx, math.Clamp(CurAng.x, -clampX, clampX), RealFrameTime()*65)
posy = math.Approach(posy, math.Clamp(CurAng.y, -clampY, clampY), RealFrameTime()*65)


	
	surface.SetDrawColor(40,40,40,120)
	surface.DrawRect(posx + 3.5, posy - 3, 255, 25)
	surface.SetDrawColor(255, 255, 255, 255)
	surface.DrawRect(posx + 4, posy, smoothHealth * 2.5, 20)
	surface.SetTextColor(255,255,255,255)
	surface.SetFont("HUDFont")
	surface.SetTextPos(posx - 60, posy - 10)
	surface.DrawText(LocalPlayer():Health())
end)


originally what I tried to do was


posx = math.Approach(math.Clamp(posx, -clampX, clampX), CurAng.x, FrameTime()*65)
posy = math.Approach(math.Clamp(posy, -clampY, clampY), CurAng.y, FrameTime()*65)


but all this does is lock the HUD assets to 0,0