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 LeftyTwoGuns · Aug 13, 2013 at 05:49 PM · projectileshoottimingalternate

Getting two barrels to fire alternately

I have a simple turret with two barrels, each able to instantiate and shoot a projectile. But I would like them to shoot alternately, like the old WW2 anti-air guns, rather than both shooting at once. Here is the script I have applied to both barrels:

  public Rigidbody bullet;
         public float power = 1500f;
         public float ShootTimer = 0.01f;
         public float nextFire = 0.0f;
         
     
         
             
             //Shooter script
         if(Input.GetButton("Fire1")&& Time.time > nextFire){
                 nextFire = Time.time + ShootTimer;
                 Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
                 Vector3 upwards = transform.TransformDirection(Vector3.up);
                 instance.AddForce(upwards * power);
             }
         
         }
     }

Tweaking my Shoot Timer and Next Fire variables doesn't really give me satisfactory results. They may alternate for a few seconds but then for some reason it appears as if they both start to shoot faster. Also letting go of the fire button and pressing it again will cause them to start firing again at the same time. Am I missing some other variables?

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

1 Reply

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

Answer by YoungDeveloper · Aug 13, 2013 at 05:59 PM

Hi, create a bool and use true as left barrel shot, and false as right barrel shot. You should save each barrel separately so you could move them without touching the other one.

 bool shootSequence;
 
 void Start(){
    shootSequence = false; //will start to shoot with right barrel, or set it as you wish
 }
 
 //if ready to shoot
 if(shootSequence){ //is true
    //shoot move/shoot left barrel
    shootSequence !=shootSequence; //change bool
 }
 
 else{ //is false
    //shoot move/shoot right barrel
    shootSequence !=shootSequence; //change bool
 }
Comment
Add comment · Show 6 · 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 LeftyTwoGuns · Aug 13, 2013 at 11:11 PM 0
Share

I've tried to set the bool up to the best of my knowledge and shooting remains the same. Here is my script, which I'm not 100% sure is correct:

 if(Input.GetButton("Fire1")&& Time.time > nextFire){
             
             if(shootSequence){
             nextFire = Time.time + ShootTimer;
             Rigidbody instance = Instantiate(bullet, aim1.transform.position, aim1.transform.rotation) as Rigidbody;
             Vector3 upwards = transform.TransformDirection(Vector3.up);
             instance.AddForce(upwards * power);
             shootSequence = !shootSequence;
             }
             
             else{
             Rigidbody instance = Instantiate(bullet, aim2.transform.position, aim2.transform.rotation) as Rigidbody;
             Vector3 upwards = transform.TransformDirection(Vector3.up);
             instance.AddForce(upwards * power);
             shootSequence = !shootSequence;
             }
         }
     
     }

It shoots just the same as before.

avatar image RomanorumLegio · Aug 13, 2013 at 11:32 PM 0
Share

It could be that you had a typo with the =! operator. Try "shootSequence =! shootSequence;" ins$$anonymous$$d of "shootSequence = !shootSequence;"

avatar image YoungDeveloper · Aug 14, 2013 at 12:01 AM 0
Share

Yea, i made a mistake by typing != ins$$anonymous$$d of =!, but LeftyTwoGuns made it right. Try is simpler for the start, without all those timers, just one click one shot.

avatar image Elroyman · Aug 14, 2013 at 12:07 AM 1
Share

As it reads I think they would both fire at the same time. I think moving the time reset out of the inner if statement will solve that.

avatar image LeftyTwoGuns · Aug 14, 2013 at 12:42 AM 0
Share

$$anonymous$$oving the ShootTimer out of the inner if statement did the trick, thanks! Fires just how I want it. And thanks for the bool suggestion, I was afraid I'd have to do some math or something.

Show more comments

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting Projectiles 1 Answer

machine gun js timing 1 Answer

Remeber the value between frame Update() 1 Answer

AI aim and shoot projectile at Player 1 Answer

I shoot a projectile, but its rotation is wrong (c#) 1 Answer


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