Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Method with WaitforSeconds into Update()

Discussion in 'Scripting' started by SteelDragonBR, Jun 3, 2015.

  1. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Hello!

    I need create an other way to call the AutoGun Method with an work WaitforSeconds into this to make the FireRate of the shots. If i call this into Update(), dont work, but i still need check in every frame.

    This my code:

    Code (CSharp):
    1. void Update ()
    2. {
    3.  
    4.         ShowInGameMenuOnKeypress ();
    5.  
    6.         if (isShot)
    7.         {
    8.          AutoGun(autoSound);
    9.         }
    10.  
    11.         GameMode ();
    12. }
    13.     IEnumerator AutoFire()
    14.     {
    15.  
    16.         if (currentBullet > 0)
    17.         {
    18.             AudioController.Play (autoSound);
    19.             StartCoroutine (PlayEffects ());
    20.             Handheld.Vibrate ();
    21.             currentBullet--;
    22.             bulletsText [GunSelect].text = currentBullet.ToString () + "/" + totalBullets.ToString ();
    23.             yield return new WaitForSeconds (fireRate);
    24.          }
    25.  
    26.         else
    27.          {
    28.             AudioController.Play ("NoAmmo");
    29.             bulletsText [GunSelect].text = "0" + "/" + totalBullets.ToString ();
    30.          }
    31.  
    32.  
    33.  
    34.  
    35.     }
    36.  
    37.     public void ButtonisDown()
    38.     {
    39.         isShot = true;
    40.         print (isShot);
    41.     }
    42.  
    43.     public void ButtonisUp()
    44.     {
    45.         isShot = false;
    46.         print (isShot);
    47.     }
    48.  
    49.     public void AutoGun(string autoShot)
    50.     {
    51.         autoSound = autoShot;
    52.  
    53.         if (gunMode == 1)
    54.     {
    55.           StartCoroutine (AutoFire ());
    56.     }
    57.  
    58.    }
     
    Last edited: Jun 3, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    SteelDragonBR likes this.
  3. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    The code working fine, i just cut it for post in here. I will fix my post. Thank you!
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Your yield return WaitForSeconds(fireRate) won't even do anything anyway since it's the last thing executed in the coroutine.
     
    SteelDragonBR likes this.
  5. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Thank you for your reply. Very helpful, but i still face problems with timer. I need calibrate this for shot 30 bullets in every 3 seconds.
     
  6. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Code (csharp):
    1. IEnumerator Fire (float fireRate) {
    2.   while (isShot) {
    3.     //your code
    4.     yield return new WaitForSeconds (fireRate);
    5.   }
    6. }
    7.  
    8. void StartShooting () {
    9.   if (!isShot) {
    10.     isShot = true;
    11.     StartCoroutine (Fire (.1f));
    12.   }
    13. }
     
    SteelDragonBR likes this.
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You don't even need a coroutine

    Code (csharp):
    1.  
    2. float lastShotTime;
    3.  
    4. void Update()
    5. {
    6.     if (Time.time - lastShotTime >= fireRate)
    7.     {
    8.         lastShotTime = Time.time;
    9.         Fire();
    10.     }
    11. }
    12.  
     
    SteelDragonBR likes this.
  8. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Thank you!
     
  9. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Flawless!

    Now Fire Rate is working! My solution with this:

    Code (CSharp):
    1.      
    2.  
    3.    voidUpdate (){
    4.  
    5.    ShowInGameMenuOnKeypress ();
    6.  
    7.     if (isShot)
    8.    {
    9.     AutoGun(autoSound);
    10.    }
    11.  
    12.     GameMode ();
    13.  
    14.  
    15.    }
    16.  
    17.     public void ButtonisDown()
    18.    {
    19.     isShot = true;
    20.     print (isShot);
    21.    }
    22.  
    23.    public void ButtonisUp()
    24.   {
    25.    isShot = false;
    26.    print (isShot);
    27.   }
    28.  
    29.   public void AutoGun(string autoShot)
    30.   {
    31.     autoSound = autoShot;
    32.  
    33.     if (gunMode == 1)
    34.     {
    35.     AutoFire ();
    36.     }
    37.  
    38.   }
    39.  
    40.   void AutoFire()
    41. {
    42.     if(Time.time - lastShotTime >= fireRate)
    43.     {
    44.  
    45.  
    46.  
    47.  
    48.          if (currentBullet > 0)
    49.          {
    50.  
    51.             AudioController.Play (autoSound);
    52.             StartCoroutine (PlayEffects ());
    53.             Handheld.Vibrate ();
    54.             currentBullet--;
    55.             bulletsText [GunSelect].text = currentBullet.ToString () + "/" + totalBullets.ToString ();
    56.             lastShotTime = Time.time;
    57.           }
    58.  
    59.          else
    60.          {
    61.           AudioController.Play ("NoAmmo");
    62.           bulletsText [GunSelect].text = "0" + "/" + totalBullets.ToString ();
    63.          }
    64.  
    65.      }
    66. }
    67.  
    68.  
     
    Last edited: Jun 3, 2015
  10. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Nope, line 11 is fine. It contains a semicolon at the end, and on the next line you have the }.