Custom projectile, collision using a tracer issue.
2 replies, posted
I've been trying to make a custom projectile and i've come across a issue.
Basically it works by checking forward of itself a certain amount of units for entities or the world, then after nothing is in its trace it'll move forward(setpos) to the end of that trace and repeat the process.
[code]
function ENT:Calculate(speed, damage, DEBUG)
local pos = self:GetPos()
local forward = self:GetForward()
local tracedata = {}
tracedata.startpos = self:GetPos()
tracedata.endpos = self:GetPos() + forward * speed
tracedata.filter = self
local trace = util.TraceLine(tracedata)
if trace.Entity:IsPlayer() then
if DEBUG then
print("HITPLAYER")
end
trace.Entity:TakeDamage(damage, self:GetOwner())
self:Remove()
elseif trace.HitWorld then
if DEBUG then
print("HitWorld")
end
self:Remove()
end
//self:SetPos(tracedata.endpos)
self:SetPos(self:GetPos() + self:GetForward() * speed)
end
local delay = 0.2
local consistant = CurTime()
function ENT:Think()
if CurTime() > consistant then
self:Calculate(self.Speed, self.DirectHitDamage, true)
consistant = CurTime() + delay
end
--self:NextThink(CurTime() + 1)
end
[/code]
Below is where the entity is created.
[code]
if (self.spawn == true) then
local disk = ents.Create(self.PROJTYPE)
disk:SetPos(eyepos + fwd * distancefromface)
disk:SetOwner(self.Weapon:GetOwner())
disk:Spawn()
disk:SetAngles(self:GetAngles() + VectorRand() * self.Recoil)
end
[/code]
I've had it kill me a few times from certain angles, but if we stand still and just shoot each other, nothing will happen, it seems to hitworld everytime though, so i suspect that the trace isn't forward?
Why not use ENT:Touch or ENT:StartTouch instead?
I'm not completely sure, but
[lua]
if (self.spawn == true) then
local disk = ents.Create(self.PROJTYPE)
disk:SetPos(eyepos + fwd * distancefromface)
disk:SetOwner(self.Weapon:GetOwner())
disk:Spawn()
disk:SetAngles(self:GetAngles() + VectorRand() * self.Recoil)
end
[/lua]
You see, you create your entity, set it's position and owner, then you spawn it.
Then you set it's angles to (self:GetAngles() + VectorRand() * self.Recoil)
First issue I see here is that you don't set the entity's angles before, so it is Angle(0,0,0), and self:GetAngles() returns Angle(0,0,0). Second issue is that you are adding an Angle to a Normalized Vector.
My speculations are that this
[lua]
disk:SetAngles((fwd + VectorRand() * self.Recoil):Angle())[/lua]
should work. I dunno though.
Sorry, you need to Log In to post a reply to this thread.