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.


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

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


Or should it be more like…

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

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
do your stuff

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.)



Someone already answered. Meh.

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

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

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” )

hook.Add( “Tick”, “KillMeIfIGetCloseToThese”, Tick )

Something like that, I guess.

Sorry, but you really over-complicated the question.

Cloud’s helping me in steam.


It’s done, besides a lua error that I can’t fix without see-ing the server that I’m on’s code.