Limiting spawning of an entity to a sphere around the player
7 replies, posted
Hi, as said in the title I want to spawn an entity with a limited radius (sphere/cylinder). I tried doing something like this:
local posCheck = ply:GetEyeTrace().HitPos
if posCheck < ply:GetAimVector() * 100 then
pos = ply:GetAimVector()
end
if posCheck > ply:GetAimVector() * 100 then
pos = ply:GetAimVector() * 100
//If you spawn the entity beyond the limiting radius it spawns on the edge of the radius
end
local example = ents.Create("ent_example")
if IsValid(example) then
example:SetPos(pos)
example:SetAngles(ply:GetAimVector():Angle())
example:SetOwner(ply)
example:Spawn()
example:Activate()
end
It doesn't work (is comparing vectors even logical here? )
Does anyone have an idea how to do it?
Maybe you need to use
local distCheck = ply:GetEyeTrace().HitPos:Distance(ply:GetEyeTrace().HitPos)
--Getting distance between TraceHit and Players Eyes
if distCheck < 100 then
pos = ply:GetAimVector()
elseif distCheck > 100 then
pos = ply:GetAimVector() * 100
//If you spawn the entity beyond the limiting radius it spawns on the edge of the radius
end
local example = ents.Create("ent_example")
if IsValid(example) then
example:SetPos(pos)
example:SetAngles(ply:GetAimVector():Angle())
example:SetOwner(ply)
example:Spawn()
example:Activate()
end
ply:GetEyeTrace().HitPos:Distance(ply:GetEyeTrace().HitPos)
What? It will always return 0.
He also wants to clamp vector values. I'd subtract hitpos vector from player position vector and then change entity spawn position vector, but I don't know if its efficient.
Ow, it must be
ply:GetEyeTrace().HitPos:Distance(ply:GetEyeTrace().StartPos)
not a
ply:GetEyeTrace().HitPos:Distance(ply:GetEyeTrace().HitPos)
That's because "ply:GetAimVector() * 100" is a local vector to the player. If you want a max distance for spawning an entity, I'd do my own trace. This is my code:
local maxSpawnDistance = 200
local spawnPos = self:EyePos() + self:EyeAngles():Forward() * maxSpawnDistance
local tr = util.TraceLine( {
start = self:EyePos(),
endpos = spawnPos,
filter = {self}
} )
if (tr.HitPos) then
spawnPos = tr.HitPos + (tr.HitNormal * Vector(10,10,10))
end
local spawned = ents.Create("fort_item")
spawned:SetItem({self.PlyInventory[slot][1],1,nil,self.PlyInventory[slot][4],self.PlyInventory[slot][5],self.PlyInventory[slot][6]})
spawned:SetPos(spawnPos)
spawned:Spawn()
self:RemoveItem(slot)
self:EmitSound("fortune/StandardDrop"..math.random( 1, 3 )..".wav")
Why not check if the entity is in a FindInSphere?
The above examples are better to use than FindInSphere tbh.
You should try to avoid FindInSphere at all costs in your code, it's pretty expensive. Also, it's not something you want to use in this context.
Sorry, you need to Log In to post a reply to this thread.