x


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

more ▼

asked May 10 '11 at 06:37 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 253

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

Just modify your existing loop a little bit:

function Timer(){
    while(true) {
        BarrelFlash();
        yield WaitForSeconds(0.3);
        currentBarrel = (currentBarrel + 1) % 4;    
    }
}
more ▼

answered May 10 '11 at 07:51 PM

superpig gravatar image

superpig
557 8 9 24

can I ask what the % 4 does?

May 10 '11 at 08:22 PM oliver-jones

This only seems to be firing barrels 1 and 3 in a loop?

May 10 '11 at 08:30 PM oliver-jones

"%" is the Mod operator http://en.wikipedia.org/wiki/Modulo_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.

May 10 '11 at 08:34 PM flaviusxvii

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?

May 10 '11 at 08:37 PM oliver-jones

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)' instead. It's a bit repetitive but for something like this it's not much longer than the alternatives.

May 10 '11 at 10:55 PM superpig
(comments are locked)
10|3000 characters needed characters left

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??

more ▼

answered May 10 '11 at 06:49 PM

joseph b gravatar image

joseph b
6 4 8 12

Well either way ... I still need help coding for the flash to move every second corresponding to the barrel. Thanks anyway

May 10 '11 at 06:53 PM oliver-jones

do you mean move forward with the barrle transform or timing wise

May 10 '11 at 07:04 PM joseph b

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

May 10 '11 at 07:12 PM oliver-jones

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.

May 10 '11 at 07:26 PM oliver-jones
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 10 '11 at 08:37 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x294
x60
x32
x24
x16

asked: May 10 '11 at 06:37 PM

Seen: 1150 times

Last Updated: May 10 '11 at 07:46 PM