Hey guys of FP, I was wondering if there is a way to make a velocity bar for a HUD? I have been experimenting with HUD making recently and I have figured out how to make an Armor and Health bar on the bottom left of my screen. Now how can I get a player velocity bar down there?
Any help appreciated. (Also, if it's possible for it to get the players velocity in the air too.
-Bradley
[URL="http://wiki.garrysmod.com/page/Entity/GetVelocity"]Entity:GetVelocity()[/URL]
[editline]15th February 2014[/editline]
Do this since you won't be able to draw a bar out of a vector..
[lua]
LocalPlayer():GetVelocity():Length()
[/lua]
Im probably going to sound really nooby at this but, how/where would I put this in my code?
My HUD Code goes as follows:
[CODE]include( 'shared.lua' )
local hud = {"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}
function GM:HUDShouldDraw(myhud)
for k, v in pairs(hud) do
if myhud == v then return false end
end
return true
end
function HUDHealth()
local ply = LocalPlayer()
local HP = ply:Health()
draw.RoundedBox( 4, 130, ScrH() - 100, 200, 40, Color( 40, 40, 40, 120 ) )
draw.RoundedBox( 4, 130, ScrH() - 100, math.Clamp( HP, 0, 200 )*2, 40, Color( 100, 0, 255, 255 ) )
draw.RoundedBox( 4, 130, ScrH() - 100, math.Clamp( HP, 0, 200 )*2, 15, Color( 255, 255, 255, 40 ) )
draw.DrawText( "Health", "arial", 130, ScrH() - 100, Color( 255, 255, 255, 255), 5 )
end
function HUDArmor()
local ply = LocalPlayer()
local Armor = ply:Armor()
draw.RoundedBox( 4, 130, ScrH() - 150, 200, 40, Color( 40, 40, 40, 120 ) )
draw.RoundedBox( 4, 130, ScrH() - 150, math.Clamp( Armor, 0, 200 )*2, 40, Color( 255, 0, 0, 255 ) )
draw.RoundedBox( 4, 130, ScrH() - 150, math.Clamp( Armor, 0, 200 )*2, 15, Color( 255, 255, 255, 40 ) )
draw.DrawText( "Armor", "arial", 130, ScrH() - 150, Color( 255, 255, 255, 255), 5 )
end
function HUDFont()
surface.CreateFont( "arial", {
font = "arial",
size = 25,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = false,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
})
end
function GM:HUDPaint()
HUDHealth()
HUDArmor()
HUDFont()
end
print("test1")[/CODE]
If by velocity you mean the players speed then surely it would be the same as the armor and health.
[lua]
function HudVelocity()
local velocity = LocalPlayer():GetVelocity():Length()
draw.RoundedBox(4, 130, ScrH() - 50, velocity, 40, Color( 0, 255, 0, 255 ))
draw.DrawText("Velocity", "arial", 130, ScrH() - 50, Color(255, 255, 255, 255), 5)
end
function GM:HUDPaint()
HudVelocity()
HUDHealth()
HUDArmor()
HUDFont()
end
[/lua]
You may have to scale the velocity bar by something as it may be a big bar depending on your speed.
Also might want to math.ceil it as it would be a float.
Use his ^
works better. Also draw the text above the box like so
[CODE]
surface.CreateFont("arial", {})
function GM:HUDPaint()
local velocity = LocalPlayer():GetVelocity():Length()
draw.RoundedBox(4, 130, ScrH() - 50, velocity, 40, Color( 0, 255, 0, 255 ))
draw.DrawText("Velocity", "arial", 130, ScrH() - 50, Color(255, 255, 255, 255), 5)
end
[/CODE]
Alright thank you both for the help. I am also wondering how I can make the bar fill up slower. And also make it so the text would say Speed = CURRENTSPEEDOFPLAYER. (Obv it doesnt say CURRENTSPEEDOFPLAYER. Thats where the speed would go :3) Overall it is working so far :)
[CODE]draw.DrawText(velocity, "arial", 130, ScrH() - 50, Color(255, 255, 255, 255), 5)[/CODE]
You'd have to edit the font to change the size
[editline]15th February 2014[/editline]
and to change the size of the bar you could just do
[CODE]draw.RoundedBox(4, 130, ScrH() - 50, velocity/4, 40, Color( 0, 255, 0, 255 ))[/CODE]
and change the velocity/4 to whatever you'd like to divide it by
No i mean like it would say how fast the player is going. Like say for example: I would be walking at the speed of 20 and next to the "Speed" text it would display 20. And if I ran or nocliped it would display 120 or something. And any idea on how to make the bar fill up slower?
[QUOTE=SubZer056;43924085]No i mean like it would say how fast the player is going. Like say for example: I would be walking at the speed of 20 and next to the "Speed" text it would display 20. And if I ran or nocliped it would display 120 or something. And any idea on how to make the bar fill up slower?[/QUOTE]
the first line i posted shows the velocity at which the player is running, and the second code made the bar smaller, as to filling up slower im not catching what you mean.
Lol you never posted the second code xD
[QUOTE=_Jacob;43924046][CODE]draw.DrawText(velocity, "arial", 130, ScrH() - 50, Color(255, 255, 255, 255), 5)[/CODE]
You'd have to edit the font to change the size
[editline]15th February 2014[/editline]
and to change the size of the bar you could just do
[CODE]draw.RoundedBox(4, 130, ScrH() - 50, velocity/4, 40, Color( 0, 255, 0, 255 ))[/CODE]
and change the velocity/4 to whatever you'd like to divide it by[/QUOTE]
So I got the velocity text to show, but what I want is so the bar speed is slower and doesnt fill up when I walk a couple steps. So say if I try and go on a surf map. The normal walkspeed when the bar fills up is 190. When I begin to surf the bar fills all the way up but the number keeps rising and the bar is already filled all the way up. How can I make it so it keeps filling up the grey as my player gets faster?
i think you have the concept wrong, draw.RoundedBox() your drawing a box not filling in any space. so because of where the "velocity" is placed you are controling the width of the roundedbox. if you want to make the box smaller, which would in theory "fill up" slower, then you would divide. If you wanted the box to "fill up" faster, or grow larger, you would multiply. Thats the best way i can explain it. and as it is, it does grow based on how fast you are going with the max speed of 1500 when you are noclipping and minimum of zero when you are stationary.
[editline]15th February 2014[/editline]
also i would recommend using [URL="http://wiki.garrysmod.com/page/math/ceil"]math.ceil[/URL] so you dont have a velocity into the millionths
You use the clamp functionality so it will only show the bar once you're past basic run speed and would also prevent it from being an extremely long bar.
Alright it works ^^ One more thing before I have this complete and I add in detail. How to display ammo? This is my code so far for ammo display:
[CODE]function HUDAmmo()
local ply = LocalPlayer()
local Ammo = ply:GetAmmoCount( 1 )
draw.RoundedBox( 4, 1630, ScrH() - 100, 200, 40, Color( 40, 40, 40, 120 ) )
draw.RoundedBox( 4, 1630, ScrH() - 100, math.Clamp( Ammo, 3, 200 )/1, 40, Color( 255, 0, 0, 255 ) )
draw.RoundedBox( 4, 1630, ScrH() - 100, math.Clamp( Ammo, 3, 200 )/1, 15, Color( 255, 255, 255, 40 ) )
draw.DrawText( "Ammo", "Trebuchet24", 1630, ScrH() - 100, Color( 255, 255, 255, 255))
end[/CODE]
Yah. Any help? It just draws the box and it doesnt move down when I shoot...
Grabbed this straight from ttt cl_hud.lua
[CODE]
local weap = ply:GetActiveWeapon()
if not weap or not ply:Alive() then return -1 end
local ammo_inv = weap:Ammo1() or 0 --Ammo in inv (not in mag)
local ammo_clip = weap:Clip1() or 0 --Ammo in current mag
local ammo_max = weap.Primary.ClipSize or 0 --Max ammo in mag
[/CODE]
Sorry, you need to Log In to post a reply to this thread.