- Home /
Making A 4 Barrel ShotGun! Need Some Help - UPDATE
Hello,
I'm making a 4 barrel shotgun turret, and basically all I want it to do is fire each barrel one second after one another. Well, when I say fire, I mean for a muzzle flash texture to appear infront of each barrel.
var currentBarrel : int = 0;
var muzzleFlash : GameObject[];
function Awake(){ muzzleFlash[0].gameObject.SetActiveRecursively(false); muzzleFlash[1].gameObject.SetActiveRecursively(false); muzzleFlash[2].gameObject.SetActiveRecursively(false); muzzleFlash[3].gameObject.SetActiveRecursively(false); }
function Start(){ Timer(); }
function BarrelFlash(){ muzzleFlash[currentBarrel].gameObject.SetActiveRecursively(true); yield WaitForSeconds(0.1); muzzleFlash[currentBarrel].gameObject.SetActiveRecursively(false); }
function Timer(){ while(currentBarrel < 4){ BarrelFlash(); yield WaitForSeconds(0.3); currentBarrel ++;
}
}
This is nearly working ... it fires all the barrels in the correct sequence, but how do I then repeat it again? So it goes from: Barrel 1 ... then 2 ... then 3 ... then 4, and then just stops, how do I keep looping?
If someone could help me out with this, then I would much appreciate it.
Thanks
Answer by superpig · May 10, 2011 at 07:51 PM
Just modify your existing loop a little bit:
function Timer(){
while(true) {
BarrelFlash();
yield WaitForSeconds(0.3);
currentBarrel = (currentBarrel + 1) % 4;
}
}
This only seems to be firing barrels 1 and 3 in a loop?
"%" is the $$anonymous$$od operator http://en.wikipedia.org/wiki/$$anonymous$$odulo_operation
It essentially gives you the remainder of division. It comes in handy when you want to constrain an integer between 1 and n - 1.
Its okay - fixed it (my bad) -- How would I go about having a WaitForSeconds after every 4 shots? So it fires 4 shots ever second, and then waits for 3 seconds and fires the 4 shots again?
Well, the easy approach would be to just write 'BarrelFlash(); yield WaitForSeconds(0.3)' four times, but on the fourth time, do WaitForSeconds(3.0)' ins$$anonymous$$d. It's a bit repetitive but for something like this it's not much longer than the alternatives.
Answer by joseph b · May 10, 2011 at 06:49 PM
you could make a prefab and just put a remove after say a 1/5 of a second and istatate it every second and make the prefab have a point light component. yes no??
Well either way ... I still need help coding for the flash to move every second corresponding to the barrel. Thanks anyway
do you mean move forward with the barrle transform or ti$$anonymous$$g wise
The barrel does not need to move. I want to know how to 'turn on' the flash game object on barrel 1, then turn off .. and do the same for barrels 2,3 and 4
I already have a GameObject of a muzzle flash infront of each barrel ... I just want to be able to turn one on after another.
Answer by flaviusxvii · May 10, 2011 at 08:37 PM
It would make more sense to have a barrel object that manages it's muzzle flash, relative to itself. Then attach 4 of these barrels to a "gun" or "barrel controller" or something with an appropriate name and function. Then have that piece fire them in sequence.
Read up on Object Oriented Programming. It is often very beneficial to recognize the objects in your program and write the code to encapsulate the individual parts. That way you could easily add a 3 or 5 barreled gun with very little work.
Your answer
Follow this Question
Related Questions
or doesn't work in while loops? 1 Answer
while(true) ??? 2 Answers
While loop crashing 2 Answers
How come my boost comes to an abrupt stop? 1 Answer
Changing a string in a loop each second 3 Answers