Detecting if player isn't looking directly at the other player
3 replies, posted
I've created this overhead hud, but have ran into problems with it not removing the avatarimage vgui. Here's a video detailing the problem i'm currently having: 2018
I've attempting to fix this by tracking the players head angle and using an if statement to see if it goes past a certain z, then remove the vgui. Although this does work, it is very buggy and 9/10 times doesn't remove the vgui. Here's my code:
local BonePos , BoneAng = LocalPlayer():GetBonePosition(LocalPlayer():LookupBone("ValveBiped.Bip01_Head1"))
if (self:GetPos():Distance(LocalPlayer():GetPos()) <= 100) then
if avatar then avatar:Remove() end
avatar = vgui.Create("AvatarImage")
avatar:SetSteamID(util.SteamIDTo64(self:SteamID()),32)
avatar:SetSize(65,65)
avatar:SetPos(xpos-150,AvatarPos)
if LocalPlayer():GetEyeTrace().Entity == self then
--draws text n such
else
if avatar then avatar:Remove() end --removes the vgui if the player isn't looking at the other
end
if BoneAng[3] < 103 then
if avatar then avatar:Remove() end --my attempt to remove the vgui using the players z head angle
end
end
Any help is appreciated!
In what function is this code? Please don't tell me you're removing and creating a VGUI on every frame.
Also, please show where AvatarPos comes from.
Checking the angle of the bone is a funny idea, because the angle you're looking for depends on the distance between you and the other player.
Also, where and how are the other things drawn? Why don't you have this issue with the transparent rect and the text?
The code is housed in this function:
plyMeta.drawPlayerInfo = function(self)
I believe it is being removed and recreated on each frame so how would I go about fixing that?
Here is the full code:
plyMeta.drawPlayerInfo = function(self)
if not IsValid(self) then return end
local pos = self:EyePos()
if(self:GetPos():Distance(LocalPlayer():GetPos()) <= 150) then
pos.z = pos.z + 1
pos = pos:ToScreen()
NamePos = pos.y - 85
AvatarPos = pos.y - 65
JobPos = pos.y - 45
License = pos.y - 5
xpos = pos.x+200
if GAMEMODE.Config.showname then
local nick, plyTeam = self:Nick(), self:Team()
local Ranked = PlayerRanks[self:GetNWString("usergroup", "")]
local BonePos , BoneAng = LocalPlayer():GetBonePosition(LocalPlayer():LookupBone("ValveBiped.Bip01_Head1"))
if (self:GetPos():Distance(LocalPlayer():GetPos()) <= 100) then
if avatar then avatar:Remove() end
avatar = vgui.Create("AvatarImage")
avatar:SetSteamID(util.SteamIDTo64(self:SteamID()),32)
avatar:SetSize(65,65)
avatar:SetPos(xpos-150,AvatarPos)
if LocalPlayer():GetEyeTrace().Entity == self then
surface.SetDrawColor(0,0,0,150)
surface.DrawRect(xpos-160,AvatarPos-10,85,85)
surface.SetDrawColor(255,255,255,200)
surface.DrawRect(xpos-160,AvatarPos-10,2,10)
surface.DrawRect(xpos-160,AvatarPos-10,10,2)
surface.DrawRect(xpos-78,AvatarPos-10,2,10)
surface.DrawRect(xpos-86,AvatarPos-10,10,2)
surface.DrawRect(xpos-160,AvatarPos+65,2,10)
surface.DrawRect(xpos-160,AvatarPos+73,10,2)
surface.DrawRect(xpos-78,AvatarPos+65,2,10)
surface.DrawRect(xpos-86,AvatarPos+73,10,2)
draw.DrawText( string.upper(nick), "HeadFont", xpos-60, NamePos, Color(50,205,50,255 ), TEXT_ALIGN_LEFT)
local teamname = self:getDarkRPVar("job") or team.GetName(self:Team())
if Ranked then
draw.DrawText(string.upper(teamname).. " | "..Ranked,"NumberFont", xpos-55, JobPos, Color(200,200,200,255), TEXT_ALIGN_LEFT)
else
draw.DrawText( string.upper(teamname),"NumberFont", xpos, JobPos, Color(200,200,200,255), TEXT_ALIGN_RIGHT)
end
if BoneAng[3] < 103 then
if avatar then avatar:Remove() end
end
else
if avatar then avatar:Remove() end
end
else
if avatar then avatar:Remove() end
end
end
end
end
Now I know it isn't the most optimized as i'm not too familiar with glua, but if you have any tips just lmk so I can improve for the future projects.
Since you want one AvatarImage panel per player, what I would do is set the avatar to self.AvatarVGUI.
if !IsValid(self.AvatarVGUI) then
self.AvatarVGUI = vgui.Create("AvatarImage")
self.AvatarVGUI:SetPlayer(self)
self.AvatarVGUI:SetSize(65,65)
self.AvatarVGUI:SetPos(xpos-150,AvatarPos)
else
self.AvatarVGUI:Show()
self.AvatarVGUI:SetPos(xpos-150,AvatarPos)
end
Also try replacing:
if BoneAng[3] < 103 then
if avatar then avatar:Remove() end
end
By:
if AvatarPos < 0 and IsValid(self.AvatarVGUI) then self.AvatarVGUI:Hide() end
Replace every "avatar" by "self.AvatarVGUI".
Replace every ":Remove()" by ":Hide()"
You should probably show/hide the vgui rather than remove/create to avoid the overhead of getting the texture setup.
Every single variable you are declaring in here should have the local keyword. When you only need a variable to exist within a function's scope, you have to make them local for performance reasons.
Related issue: Putting a players steam avatar in a hud?
Sorry, you need to Log In to post a reply to this thread.