How do make a entity look of the player closest to itself
[lua]
local closest;
for k, v in pairs( player.GetAll() ) do
local position = entity:GetPos();
if ( IsValid(closest) and closest:GetPos():Distance(position) > v:GetPos():Distance(position) ) then
closest = v;
elseif ( !IsValid(closest) ) then
closest = v;
end;
end;
[/lua]
[QUOTE=Chessnut;38230387][lua]
local closest;
for k, v in pairs( player.GetAll() ) do
local position = entity:GetPos();
if ( IsValid(closest) and closest:GetPos():Distance(position) > v:GetPos():Distance(position) ) then
closest = v;
elseif ( !IsValid(closest) ) then
closest = v;
end;
end;
[/lua][/QUOTE]
ok i don't think i am going the right direction here . I am trying to make a spinning top that fallows players
hers what i have done that has your code as well
[lua] function ENT:Initialize()
self.Entity:SetModel( "models/top/top.mdl" )
self.Entity:PhysicsInitSphere( 10, "ice" )
local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
phys:SetMass( 250 )
end
end
function ENT:SpawnFunction( ply, tr )
if ( !tr.Hit ) then return end
local SpawnPos = tr.HitPos + tr.HitNormal * 16
local ent = ents.Create( "pop_top" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
end
function ENT:Think()
local closest;
for k, v in pairs( player.GetAll() ) do
local position = self.Entity:GetPos();
if ( IsValid(closest) and closest:GetPos():Distance(position) > v:GetPos():Distance(position) ) then
closest = v;
elseif ( !IsValid(closest) ) then
closest = v;
end;
end;
local getobj = self.Entity:GetPhysicsObject()
getobj:AddAngleVelocity(Vector(0,0,99))
getobj:ApplyForceCenter(Vector(closest:GetPos()))
//print (closest:GetPos())
end
function ENT:PhysicsCollide( data, physobj)
end
function ENT:Use( activator, caller )
end
function usereply(self)
end
[/lua]
do you know what i am doing wrong?
Function that will help you to get closest player to some vector.
[PHP]
function dist( vec1, vec2 )
return ( vec1[1] + vec1[2] + vec1[3] ) - ( vec2[1] + vec2[2] + vec2[3] )
end
function GetClosest( vec )
local pos, ply = nil
for _, pl in ipairs( player.GetAll() ) do
if dist( pl:GetPos(), vec ) < ( pos or 999999999 ) then
pos = dist( pl:GetPos(), vec )
ply = pl
end
end
return ply
end
[/PHP]
[QUOTE=gravelhunter;38231293]getobj:ApplyForceCenter(Vector(closest:GetPos()))[/QUOTE]
I might be wrong, but I'm sure that applies force in the direction the vector's pointing
You'll need to get the direction of the player relative to the entity, which should (I think) be something along the lines of
[lua]local vector = self:WorldToLocal( closest:GetPos() )[/lua]
Sorry, you need to Log In to post a reply to this thread.