Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · May 10, 2011 at 06:37 PM · loopwhilewhile-loopsequencemuzzleflash

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;    
    }
}
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · May 10, 2011 at 08:22 PM 0
Share

can I ask what the % 4 does?

avatar image oliver-jones · May 10, 2011 at 08:30 PM 0
Share

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

avatar image flaviusxvii · May 10, 2011 at 08:34 PM 0
Share

"%" 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.

avatar image oliver-jones · May 10, 2011 at 08:37 PM 0
Share

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?

avatar image superpig ♦♦ · May 10, 2011 at 10:55 PM 0
Share

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.

avatar image
-1

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · May 10, 2011 at 06:53 PM 0
Share

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

avatar image joseph b · May 10, 2011 at 07:04 PM 0
Share

do you mean move forward with the barrle transform or ti$$anonymous$$g wise

avatar image oliver-jones · May 10, 2011 at 07:12 PM 0
Share

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

avatar image oliver-jones · May 10, 2011 at 07:26 PM 0
Share

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.

avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges