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

How to implement reload times in my autoturret script?

Discussion in 'Scripting' started by GuyStreamsStuff, Apr 23, 2018.

  1. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    So I got a turret that fires 3 rounds and then it has to pause for a second or two to reload, however I have no idea how to implement some kind of wait time inside the coroutine to make it work. I tried yield return WaitforSeconds kind of everywhere but without success. Sorry I'm new to coding and my research didn't give me the results I was looking for. Thanks for your patience!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class burstshoot : MonoBehaviour
    6. {
    7.  
    8.     public Transform player;
    9.     public GameObject bullet;
    10.     public float numberOfBullets;
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.tag == "Player")
    15.         {
    16.             transform.LookAt(player);
    17.             StartCoroutine(Shoot());
    18.         }
    19.     }
    20.  
    21.     private void OnTriggerStay(Collider other)
    22.     {
    23.         if (other.tag == "Player")
    24.         {
    25.             transform.LookAt(player);
    26.  
    27.  
    28.         }
    29.     }
    30.  
    31.     IEnumerator Shoot()
    32.     {
    33.         for (int i = 0; i < numberOfBullets; i++)
    34.         {
    35.             Instantiate(bullet, transform.position, transform.rotation);
    36.  
    37.             yield return new WaitForSeconds(0.2f);
    38.  
    39.  
    40.         }
    41.  
    42.  
    43.  
    44.     }
    45.  
    46.     private void OnTriggerExit(Collider other)
    47.     {
    48.         if (other.tag == "Player")
    49.         {
    50.             StopCoroutine(Shoot());
    51.         }
    52.     }
    53. }
    54.  
     
  2. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    Give this a try
    Code (CSharp):
    1.     IEnumerator Shoot( )
    2.     {
    3.         for( int i = 0; i < numberOfBullets; i++ )
    4.         {
    5.             Instantiate ( bullet, transform.position, transform.rotation );
    6.         }
    7.         yield return new WaitForSeconds ( 2f );
    8.     }
     
  3. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    I already tried that but then my turret only shoots every 2 seconds. I want it to shoot a burst of numberOfBullets before pausing for a second and then reshoot numberOfBullets again, indefinitely.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    put the whole code in a loop and pause inside the inner loop for .2f and outside of the inner loop, for 2s.
    Code (csharp):
    1. IEnumerator Shoot() {
    2.    while(true)
    3.    {
    4.       for(int i = 0; i < numberOfBullets; ++i)
    5.       {
    6.          Instantiate(bullet, transform.position, transform.rotation);
    7.          yield return new WaitForSeconds(0.2f);
    8.       }
    9.     yield return new WaitForSeconds(2);
    10.     }
    11.  }
    You can also cache the WaitForSeconds*.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh, btw, you should start/stop the coroutine like this:
    Code (csharp):
    1. Coroutine shootRoutine;
    2.  
    3. // upon entering the trigger
    4. shootRoutine = StartCoroutine(Shoot());
    5.  
    6. // when exiting the trigger
    7. if(shootRoutine != null) StopCoroutine(shootRoutine);
     
  6. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    How about this then:

    Code (CSharp):
    1.     public Transform player;
    2.     public GameObject bullet;
    3.     public float numberOfBullets;
    4.     private IEnumerator shootCoroutine;
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.     {
    8.         if (other.tag == "Player")
    9.         {
    10.             transform.LookAt(player);
    11.             if (shootCoroutine != null)
    12.             {
    13.               StopCortouine(shootCoroutine);
    14.               shootCoroutine = null;
    15.             }
    16.             shootCoroutine = Shoot()
    17.             StartCoroutine(shootCoroutine);
    18.         }
    19.     }
    20.     private void OnTriggerStay(Collider other)
    21.     {
    22.         if (other.tag == "Player")
    23.         {
    24.             transform.LookAt(player);
    25.         }
    26.     }
    27.  
    28.     IEnumerator Shoot()
    29.     {
    30.        while (true)
    31.        {
    32.           for (int i = 0; i < numberOfBullets; i++)
    33.           {
    34.             Instantiate(bullet, transform.position, transform.rotation);
    35.              yield return new WaitForSeconds(0.5f);
    36.           }
    37.        }
    38.     }
    39.     private void OnTriggerExit(Collider other)
    40.     {
    41.         if (other.tag == "Player")
    42.         {
    43.             StopCoroutine(shootCoroutine);
    44.             shootCoroutine = null;
    45.         }
    46.     }
    47. }
    48.  
    You want to loop indefinitely inside the coroutine. The code above also shows how to properly stop a coroutine.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You just forgot the 2s delay inside the (outter) loop. :)