So I've pretty much fallen in love with the amazing WAC Little Bird Helicopter, and I decided to make my own variant for personal use with a number of exotic weapons. One of these was supposed to be a rocket swarm that would shoot 12 rockets all at once with slightly different trajectories to give them a bit of a spread. The only problem is that when I fire, all the rockets instantly explode and destroy the chopper. I think it's because they're being spawned at the same spot and they're colliding, but I don't know how to nocollide them all. Here's the relevant code snippet.
[lua]
[1]={
Name="Rocket Swarm",
Ammo=2,
MaxAmmo=2,
NextShoot=1,
LastShot=0,
ShootDelay=1,
Gun=1,
ShootPos={
[1]=Vector(50,70,-20),
[2]=Vector(50,-70,-20),
},
func=function(self,t,p)
if t.NextShoot<=CurTime() then
if t.Ammo>0 then
for shoot=0, 12, 1 do
local rocket=ents.Create("wac_hc_rocket")
rocket:SetPos(self:LocalToWorld(t.ShootPos[t.Gun]))
rocket:SetAngles(self:GetAngles()*Vector(0,0,math.random(0.9,1.1)))
rocket.Owner=p
rocket.Damage=150
rocket.Radius=200
rocket.Speed=60
rocket.TrailLength=100
rocket.Scale=7
rocket.SmokeDens=1
rocket.Launcher=self
rocket:Spawn()
rocket:StartRocket()
local ph=rocket:GetPhysicsObject()
if ph:IsValid() then
ph:SetVelocity(self:GetVelocity())
end
self.Sound.MissileShoot:Stop()
self.Sound.MissileShoot:Play()
constraint.NoCollide(self,rocket,0,0)
constraint.NoCollide(rocket,rocket,0,0)
t.Gun=(t.Gun==1 and 2 or 1)
t.LastShot=CurTime()
t.NextShoot=t.LastShot+t.ShootDelay
end
t.Ammo=t.Ammo-1
end
if t.Ammo<=0 then
t.Ammo=t.MaxAmmo
t.NextShoot=CurTime()+10
end
end
end,
},
[/lua]
I should also mention that I'm something of a novice lua coder, but I'm learning as best I can.
This is just going to spawn a bunch of rockets all at once. You need to do your spawning over a time period instead of just sticking it into a for loop. With this current spawning wether you have 1 or 12 there all going to the same area.
Here is how to no collide, it's pretty self explanitory.
[url]http://wiki.garrysmod.com/?title=Constraint.NoCollide[/url]
and when debugging your stuff, lower the number down instead of using twelve you could start at one and move up slowly from there.
Okay. Is there a way I can put a delay in the loop the make the rockets fire sequentially?
Something kind of like this
[LUA]
local function FireRocket(current)
--Do rocket stuff here and fire one
current = current + 1
if(current>12)then return end
timer.Simple(0.5, FireRocket, current)--Set the timer for the next rocket
end
if t.NextShoot<=CurTime() and t.Ammo>0 then
local current = 1
timer.Simple(0.5, FireRocket, current)
end
[/LUA]
Sorry, you need to Log In to post a reply to this thread.