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 Time4Panda · Dec 22, 2014 at 07:47 PM · fpsyieldwaitforsecondsguns

Yield WaitForSeconds only works sometimes?!

Ok so I'm fairly new to the Unity community and I'm trying to begin some sort of FPS, but something is just not working. After my reload animation, if I shoot (full auto) any time within like 2 seconds of when the reload animation ended, I can shoot millions of bullets out rapidly as if the WaitForSeconds as decided to not work. If I wait more than 4 seconds after reload, it works just fine with an even automatic fire. Help?!

Heres my script:

 var Bullet : Rigidbody;
 var BulletSpeed : float = 1000;
 var ReloadTime : float = 2.2;
 var Ammo : float = 30;
 var Spawn : Transform;
 var IsFullAuto = true;
 
 
 @script HideInInspector
 
 static var ReloadTTime : float;
 static var IsReloading = false;
 static var AmmoLeft : float;
 private var CanFire = true;
 
 function Start () {
     AmmoLeft = Ammo;
     ReloadTTime = ReloadTime;
 }
 
 function Update () {
 
         if(Input.GetButton("Fire1")){
             if(AmmoLeft > 0){
                 if(CanFire == true && IsReloading == false){
                     BroadcastMessage("FireAnim");        
                         Fire();
             }
         }
     }
 
     if(AmmoLeft == 0)
     {
         BroadcastMessage("ReloadAnim");
         Reload();
     }
     
     if(AmmoLeft < 0){
         AmmoLeft = 0;
     }   
          
     
 }                                                                    
                                                                                                                                                                                                 
 function Fire(){
     
     var FireRate = 0.2;
     CanFire = false;
     yield WaitForSeconds(FireRate);
     CanFire = true;
     var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
     bullet1.AddForce(Spawn.forward * BulletSpeed);
     AmmoLeft -= 1;
     audio.Play();
 }
     
 
 function Reload(){
     CanFire = false;
     IsReloading = true;
     yield WaitForSeconds(ReloadTime);
     AmmoLeft = Ammo;
     IsReloading = false;
     CanFire = true;
 }

Comment
Add comment · Show 2
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 meat5000 ♦ · Dec 22, 2014 at 07:52 PM 0
Share

yield will stop the execution of the coroutine, continuing its execution after the yield instruction, the next frame or your specified wait time.

I'm guessing you know this but you are missing the consequences.

Rethink your logic as it appears that your flags are beco$$anonymous$$g set in an undesirable way as a result.

avatar image Professor Snake · Dec 22, 2014 at 10:38 PM 0
Share

I feel like WaitForSeconds is to be avoided unless absolutely necessary, especially in situations like this. Not a proper answer to your question as it doesn't point out the flaws in the code logic, but i recently answered this which might help you rewrite it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Taxen0 · Dec 23, 2014 at 10:40 AM

Try the following changes, but as other have stated I too usually try and stay away from yield. in this case you could instead have a variable that stores the last time you fired and then compare it to the current time and see if the defference is larger than the firerate. But both ways should work fine =)

I usually code in c# so if i missed anything read more about this here

  function Update () {
  
          if(Input.GetButton("Fire1")){
              if(AmmoLeft > 0){
                  if(CanFire == true && IsReloading == false){
                      BroadcastMessage("FireAnim");        
                          yield StartCoroutine(Fire());
              }
          }
      }                                                 
  }

                                                                                                                                                                                             
  function Fire(){
      
      var FireRate = 0.2;
      CanFire = false;
      var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
      bullet1.AddForce(Spawn.forward * BulletSpeed);
      AmmoLeft -= 1;
      audio.Play();
      yield WaitForSeconds(FireRate);
      CanFire = true;

  }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Waiting between pingpong loops 2 Answers

WaitForSeconds not working C# 2 Answers

What is wrong with this use of WaitForSeconds? 1 Answer

waitforseconds in update good for perfomance ? 2 Answers

yield WaitForSeconds makes GUI button disappear 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