Player proximity

HOW can i make a function that says:
If Ent:IsCloseToPlayer()(lets say like, 6 Feet or so)
then self.Entity:CreateImpact()
end

Createimpact is something fully coded, dont worry about that.
my reason for askin is, is i made a Homing Missile launcher with LUA and its being used to try and hit flying, quick moving targets. right now, it will only Explode and cause damage when it hits them Directly. problem is, is that these missles are NO WHERE NEAR accurate enough to actually HIT a fast,moving,and flying target. so im making it so that it Detonates when it gets close enough.

Close to any player or close to who you’re homing to? If it’s the latter:

[lua]
if self:GetPos():Distance(self.HomingTarget:GetPos()) < 50 then
self:CreateImpact()
–or whatever you want to do to explode
end
[/lua]

Also, GetPos() for players would be at the bottom of their feet. If you want to use the player’s eye position, use this instead:



if self:GetPos():Distance(self.HomingTarget:GetShootPos():Add(self.HomingTarget:GetAimVector())) < 50 then
    self:CreateImpact()
end


Or just the player’s shoot position, where bullets actually come out of (in the middle of their forehead, where you’d aim if you were trying to headshot them):



if self:GetPos():Distance(self.HomingTarget:GetShootPos()) < 50 then
    self:CreateImpact()
end


alright that all makes perfect sense, but i might have a problem with Self.HomingTarget. i might just be Derping up on a little issue, but im tired and i dont care.
[lua]
function ENT:Think()

-- If it touches the water it will explode
if ( self.Entity:WaterLevel() &gt; 0 ) then
	self.Entity:CreateImpact()
	
	-- Water Explosion effect
	local effectdata = EffectData()
	effectdata:SetStart( self.Entity:GetPos() )
	effectdata:SetOrigin( self.Entity:GetPos() )
	effectdata:SetScale( 1 )
	util.Effect( "gunshotsplash", effectdata )	
end

-- If our Enemy is not Valid, then Enemy is nil, and distance is reset
if ( !ValidEntity( Enemy ) || Enemy == self.Entity:GetOwner() ) then
	Enemy = nil
	NilDistance = math.huge
end

-- Getting all of the entities on the Map
for k,v in pairs( ents.GetAll() ) do

	-- If the entity found is an NPC or a Player its good to continue
	if ( v:IsPlayer() || v:IsNPC() ) then
	
		-- If the Player in question is us, then its not a good enemy
		if !( v == self.Entity:GetOwner() ) then
		
		-- Just getting the distance between us and the enemy
		local EnemyDistance = self.Entity:GetPos():Distance( v:GetPos() )
			
			-- If an enemy is closer than the other, we target him
			if ( EnemyDistance &lt; NilDistance ) then
				NilDistance = EnemyDistance
				Enemy = v
			end
		end
	end
end

-- If the enemy is nil or not visible, then its not good, else, we set our projectile to homing phase
if !( Enemy == nil || !self.Entity:VisibleTarget( Enemy ) ) then
	self.Entity:PointAtEntity( Enemy )
	self.Entity:GetPhysicsObject():ApplyForceCenter( self.Entity:GetForward() * 1600 )

– Copy pasted code
if self:GetPos():Distance(self.HomingTarget:GetShootPos()) < 50 then
self:CreateImpact()
end
–end copy pasted code
end

-- Make the entity think
self.Entity:NextThink( CurTime())
return true

end
[/lua]

THAT is my targeting system.
i THINK i might be able to use self.Enemy:GetPos or GetShootpos.
if not please correct me =p
By the way, what vector should i use for the MIDDLE of the body? maybe look up the spine? =p

If you want to get the position of the torso the easy way is just to add 36 vertical units to the entity pos, since humanoid characters are about 72 units high. But if you home at a small NPC like a headcrab it’ll home into a point in the air above the crab. Considering you’re using PointAtEntity, I don’t think you have the ability to aim at a specific point.

It looks like you’re using a global variable (Enemy) to remember what you’re homing at, which you shouldn’t. If you want to store data specific to the entity (such as what you’re homing at) make it a member of the entity eg self.Enemy. Same for NilDistance. If you have two of these missiles at once they’ll use the same NilDistance and Enemy, conflicting with eachother.

In the example I gave, self.HomingTarget was just the entity you’re aiming at - in your case this is Enemy, or self.Enemy as it should be.

As a last thing, self.Entity is deprecated - you can just use self instead.

huh. for the headcrab thing i will add a NPC table and the vectors to get them.
also, Self.Entity still works. so why change it?

[editline]01:57AM[/editline]

oh and, how exactly would i add 36 Y units to the aim vector?