Making an entity 'track' a player

So I’m attempting to have an entity face/track a player as he/she moves using the following code

(This code isn’t exactly what I’m using. I’ve changed small bits to help readability)



local AngDifference =Angle((self:GetPos().y - CurrentTarget:GetPos().y) - self:GetAngles().y)

-- Turn Left
if AngDifference > 181 and AngDifference < 360 then
	self:SetAngles(Angle(self:GetAngles().p, self:GetAngles().y + 1, self:GetAngles().r))
-- Turn Right
elseif AngDifference < 179 and AngDifference > 0 then
	self:SetAngles(Angle(self:GetAngles().p, self:GetAngles().y - 1, self:GetAngles().r))
end

Now I’m sure that I’m missing some basic principal of this type of thing. But this tracks just fine, until I pass a specific spot on the Y axis. At which point, depending on the direction I pass through, AngDifference will return 500+ or 30 or less(rough numbers). Anybody have some input? Maybe a better way to achieve this or somewhere to look for information. Any help would be appreciated and if any more information is needed I can provide it.

Face at



local ang = (CurrentTarget:GetPos()-self:GetPos()):Angle()
self:SetAngles(ang)


Only yaw



local ang = (CurrentTarget:GetPos()-self:GetPos()):Angle()
local ang2 = self:GetAngles()
self:SetAngles(Angle(ang2.p,ang.y,ang2.r))


Smooth yaw (Not tested and I might be wrong as I never used mod in Gmod)



local yaw = (CurrentTarget:GetPos()-self:GetPos()):Angle().y
local myyaw = self:GetAngles().y
local turnspeed = 4
local AngleDiff = math.fmod(((math.fmod(myyaw - yaw,360)) + 540),360) - 180

local ang2 = self:GetAngles()
self:SetAngles(Angle(ang2.p,myyaw-math.Clamp(AngleDiff,-turnspeed,turnspeed),ang2.r))

So far that works fantastically =) Thanks so much Nak