 # Detecting distance from an entity.

How do I do this?

Being, if I was to, say, run up to a barrel and my distance is 5 from it or something, then run other code. Otherwise don’t.

ents.FindInSphere(Vector(),howbigthesphereis(flatnumbernotvector))

That isn’t helpfull for me. :l… But from that, I get…

``````

where = ents.FindInSphere(Vector(),33(what?))

if where = true then
Do lua shit here.
else end
end

``````

Where ‘what?’ is where I have no idea wtf that part of the code was for. Please do explain .

EDIT:

Or should it be more like…

``````

for k, v in pairs(ents.FindInSphere(Vector(),33(what?))) do
Do lua shit here.
end

``````

And where in that is the part for writing what entity needs to be found?

>.>…

Ok for an entity if your building a custom entity an easy thing to do it.

``````
function ENT:Think()
local where= ents.FindInSphere(Vector(0,0,0),32)
for k,v in pairs(where) do
if v != self.Entity then
end
end
end

``````

You could also do this with any vector location and not inside an entity by putting this in a timer or a servers think. If you simply need to compare distances between 2 known objects (keep in mind ents find in sphere is only registering entities that it finds iwthin a distance this other calculation would end up running non stop checking distances for what your example wants aka think or timers.)

``````
Vector(0,0,0):Distance(Vector(100,100,100))

``````

-snip-

I’m not MAKING an entity. This is a clientside thing, for example, to run kill on myself, if I got near some - entity.

[lua]
local things, range

range = 128

things = {
prop_physics = true,
player = true,
weapon_crowbar = true
}
local function Tick( )
local k, v, pl, pos
pl = LocalPlayer( )

if not ValidEntity( pl ) or not pl:Alive( ) then
return
end

pos = pl:GetPos( )
for k, v in ipairs( ents.GetAll () ) do
if v:GetPos( ):Distance( pos ) <= range and v ~= pl then
if things[ v:GetClass( ) ] then
RunConsoleCommand( “kill” )
break
end
end
end
end