How do I draw text on on my trace entity?

I want it so when I look at my trace entity( in this case a player ), it will draw text above their head, how would I do this?

[lua]local pos = TARGET:GetPos()
pos.z = TARGET:LocalToWorld(TARGET:OBBMaxs()).z
pos = pos:ToScreen()

draw.SimpleText( TARGET:Nick(), “TargetID”, pos.x, pos.y, Color(0,0,0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )[/lua]

TARGET is the trace entity, this is untested but it should work.

I was looking for something just like this, I worked out how to draw a players name when you look at them but it wasn’t relative to their actual position on the level. How could I modify this script to show a players name whether they are being looked at or not?

[lua]function drawPlayerNames()
// Sets the trace variables
local trace = util.TraceLine(util.GetPlayerTrace(LocalPlayer()))

// Check if trace has hit a player
if (ValidEntity(trace.Entity)) then
	if (trace.Entity:IsPlayer()) then
		local target = trace.Entity
		local targetPlayerName = trace.Entity:Name()
		// Set text position
		local pos = target:GetPos()
		pos.z = target:LocalToWorld(target:OBBCenter()).z
		pos = pos:ToScreen()
		// Draw the name
		draw.SimpleTextOutlined(targetPlayerName, "TargetID", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0, 0, 0, 255))


If you want it to draw everyone’s name no matter what just use
[lua]for _,target in pairs(player.GetAll()) do --etc.[/lua]

to loop through all the players and draw each of their names.

Thanks once again CowThing. Works perfectly.

For anyone that wants to know how to do this perfectly, here is the code, it will only draw if your close to them, and aiming at them as well

function GM:HUDPaint( )
	if( ValidEntity( LocalPlayer() ) ) then
		local trace = { }
		trace.start = LocalPlayer():EyePos();
		trace.endpos = trace.start + LocalPlayer():GetAimVector() * 300;
		trace.filter = LocalPlayer();
		local tr = util.TraceLine( trace );
		if( ValidEntity( tr.Entity ) and tr.Entity:IsPlayer() ) then
			local pos = tr.HitPos:ToScreen();
			draw.DrawText( tr.Entity:GetNWString( "Name" ), "ChatFont", pos.x, pos.y, Color( 255, 255, 255, 255 ) );
			draw.DrawText( tr.Entity:GetNWString( "Description" ), "ChatFont", pos.x, pos.y + 20, Color( 255, 255, 255, 255 ) );
		if( ValidEntity( tr.Entity ) and tr.Entity:IsNPC() ) then
			local pos = tr.Entity:GetPos()
			pos.z = tr.Entity:LocalToWorld(tr.Entity:OBBMaxs()).z
			pos = pos:ToScreen()
			draw.SimpleText( tr.Entity:GetClass(), "TargetID", pos.x, pos.y, Color(0,0,0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )

This is a piece from my roleplay gamemode, this will draw NPC’s class above their head, and text above players heads.

Thanks CowThing :slight_smile:

you could combine yours with this 3d text I made.

surface.CreateFont ("HUDNumber5", ScreenScale(128), 400, true, false, "FarmFont")

function GM:PostDrawTranslucentRenderables( )  
    local pos = LocalPlayer():GetShootPos()
    local ang = LocalPlayer():GetAimVector()
    local newpos = pos+(ang*80)

    for k,v in pairs(player.GetAll()) do
        if !v:Alive() then return end
        cam.Start3D2D( v:GetPos()+Vector(0,0,85), Angle(0,LocalPlayer():EyeAngles().y-90,90),.1 )
            if v != LocalPlayer() then        
                draw.DrawText(v:Nick(), "FarmFont", 0, 0, Color(20, 200, 20, 230), TEXT_ALIGN_CENTER )
        cam.Start3D2D( v:GetPos()+Vector(0,0,85), Angle(0,LocalPlayer():EyeAngles().y+90,90),.1 )
            if v != LocalPlayer() then
                draw.DrawText(v:Nick(), "FarmFont", 0, 0, Color(20, 200, 20, 230), TEXT_ALIGN_CENTER )