How can I make it so when I look at an NPC, It shows text right above their head? Its not 3d2d, its 2d. But its in the world.
[QUOTE=Brodster55;19906227]How can I make it so when I look at an NPC, It shows text right above their head? Its not 3d2d, its 2d. But its in the world.[/QUOTE]
Then you'd need to know where it is in 3D.
For starters you need to add some painting functions so we'll need the [b][url=wiki.garrysmod.com/?title=Gamemode.HUDPaint]Gamemode.HUDPaint [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] hook.
Then we'll need to know what entity we're looking at, so we need the [b][url=wiki.garrysmod.com/?title=Player.GetEyeTrace]Player.GetEyeTrace [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and [b][url=wiki.garrysmod.com/?title=G.LocalPlayer]G.LocalPlayer [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. If you want this to only work on NPCs you'll need this as well. [b][url=wiki.garrysmod.com/?title=Entity.IsNPC]Entity.IsNPC [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
For the actual displaying of the information you'll need [b][url=wiki.garrysmod.com/?title=Draw.DrawText]Draw.DrawText [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and [b][url=wiki.garrysmod.com/?title=Vector.ToScreen]Vector.ToScreen [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[lua]hook.Add("HUDPaint", "ShowNPCHealthAboveHead", function()
-- Get the entity we're looking at
local ent = LocalPlayer():GetEyeTrace().Entity
-- check if it's really an NPC
if ValidEntity(ent) and ent:IsNPC() then
-- change the Vector in LocalToWorld to change
-- where the text should appear in relation to the ent
local pos = ent:LocalToWorld(Vector(0, 0, 100):ToScreen()
-- check we can actually see this part of the entity
if pos.visible then
-- what info to draw
draw.DrawText(tostring(ent:Health()),
-- what font to draw it in
"ScoreboardText",
-- where to draw it
pos.x, pos.y,
-- what colour it must be
Color(255, 255, 255),
-- how to align the text
ALIGN_CENTER
)
end
end
end)[/lua]
I tried doing this...
[lua]
hook.Add("HUDPaint", "Hud", function()
-- Get the entity we're looking at
local ent = LocalPlayer():GetEyeTrace().Entity
-- check if it's really an NPC
if ValidEntity(ent) and ent:IsNPC() then
-- change the Vector in LocalToWorld to change
-- where the text should appear in relation to the ent
local pos = ent:LocalToWorld(Vector(0, 0, 100):ToScreen())
-- check we can actually see this part of the entity
if pos.visible then
-- what info to draw
draw.DrawText(tostring(ent:Health()),
-- what font to draw it in
"ScoreboardText",
-- where to draw it
pos.x, pos.y,
-- what colour it must be
Color(255, 255, 255),
-- how to align the text
ALIGN_CENTER
)
end
end
end)
[/lua]
got this error
Hook 'Hud' Failed: nggrp1/gamemode/cl_init.lua:331: bad argument #1 to 'LocalToWorld' (Vector expected, got table)
local x,y = ent:LocalToWorld(ent:GetPos() + Vector(0,0,100)):ToScreen()
Close :l
Ahh, didn't see it there..
Thanks!
How can you do it so the player has to be within a certain amount of units to see it?