Need help making an entity face or track a player or npc

So I’m attempting to have an entity face-track a player or npc as he/she moves . Is there a simple way of doing this, I have done some experimenting and got it to only point once towards an entity spawned using ent.Create, but it doesn’t update its angles when i move the entity around. I also have no idea how i would make said entity target an npc or player. How would I getPos of the npc or player and make the entity update its angles to face the npc or player as it moves around? Is there any example code I could have a look at or mods that do this? Any help would be appreciated!!

Heres some of my experimenting code, Points the entity at the created entity, only once doesn’t update…


function ENT:Point() 
	
local PTACH = ents.Create( "gmod_button" )
	if ( !IsValid( PTACH ) ) then return end  --Check whether we successfully made an entity, if not - bail
	PTACH:SetModel( "models/dav0r/buttons/button.mdl" )
	PTACH:SetPos( Vector( 0, 0, 100 ) )
	PTACH:Spawn()

	local pos= self:GetPos()
    local PTACH = PTACH:GetPos()
    local ang = ( PTACH - pos):Angle()
	self:SetAngles(ang)
end

There’s no need to create another entity (your solution wouldn’t work anyway since you are not storing the entity to any variable so it will be garbage collected). Instead, you can follow a player’s face by setting the entity’s angle to the angular translation of the difference between the player’s eyepos and the entity’s position. Ex.


self:SetAngles((Player:EyePos() - self:GetPos()):Angle())

Bearing in mind that i’m new to lua and a complete nub, that code returns with the error attempt to index global ‘Player’ (a function value) which I understand isn’t suppose to be used under an ENT function, but idk how to get the player, like what is the players name in code, like if I want to get the position of the entity currently referred to in the code I would say self.GetPos(). But i couldn’t say player.GetPos lol…

This probably looks stupid lol


function ENT:Point()
	
self:SetAngles((Player:EyePos() - self:GetPos()):Angle())
end

Also what if i wanted my entity to target an npc and face it, That would probably be something complex… Idk how to get the pos of an npc, i only know how to use some of the simple functions currently in gmod wiki, but theres no NPC.GetPos() I would probably have to make my own function to get the pos of an npc which is out of my league…

[editline]26th May 2017[/editline]

What would be even better and much more advanced is to have the entity be able to wire to the target finder from wire-mod and constantly face towards whatever the target finder targets. But there is absolutely no tutorials whatsoever on anything like that. You have to have full understanding on how to code in lua i’m assuming to do that…

NPC inherits from Entity functions, so GetPos is available to it. Player was just an example, but if you want to target a player, use

ents.FindInSphere to find the closest one and re-search if the player has left the radius. Your description highly depends on implementation and will require you to experiment to get the result you want.

Yea, I have tried experimenting with ents.findinsphere, The entity I have currently is a turret that fires rounds when you press E on it. It also has wire inputs. But i’m just trying to make it target players and npcs automatically so I could simply call my fire function when it is facing a player or npc so it shoots them.
Im sure its something simple, less than 10 lines of code, just can’t figure it out lol. Literally just experimenting with a plain box with no other functions just init, just simply trying to make it track the player or npc and if I get the box to track something i can just transfer the code to my turret.Well thanks for your help ill mess with it some more.

[editline]27th May 2017[/editline]

So here is what I figured out so far which I feel is close to what I want but it gives the following error

[ERROR] lua:301: bad argument #1 to ‘__sub’ (Vector expected, got userdata)

  1. __sub - [C]:-1

Here is the code


function ENT:Point() 
	for k,v in pairs(ents.FindInSphere(self:GetPos(), 200)) do
		if(v:GetClass() == "player") then
			
		local pos= self:GetPos()
		local ang = ( v - pos):Angle()
		self:SetAngles(ang)
			end
		end

end


[editline]27th May 2017[/editline]

DERRRP… got it to work


 function ENT:Point() 
	for k,v in pairs(ents.FindInSphere(self:GetPos(), 200)) do
		if(v:GetClass() == "player") then
		
		local playerpos = v:GetPos()
		local pos= self:GetPos()
		local ang = ( playerpos - pos):Angle()
		self:SetAngles(ang)
			end
		end

end

However, How would i make it continually update it’s angles as the player moves about, so far it only points to the player once, doesn’t track it

Call ENT.Point in a Think hook or something. Also, use IsPlayer instead of a GetClass comparison.

Thank you for your help, that works perfectly, I should have thought of that since the think function loops :stuck_out_tongue: Now the only weird thing is the entity has slow physics while tracking, for example I spawn it, and try to throw it with phys gun, it will just kinda float there and slowly fall to the ground like a feather lol. Changing the angle seems to freeze its physics for a second it seems like.