Search Unity

Rapid Fire for 10 Seconds

Discussion in 'Scripting' started by tburns517, Feb 7, 2016.

  1. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    Hello everyone,

    I am about a month into using Unity. I have been following along with Unity tutorials and I am working on upgrading the Space Shooter. I am simply creating a pick up item which drops down from a random position like the asteroids. I have created a script for this, but it does not seem to update or change the fire rate of the shots after the object is picked up. The fire rate is in the Player Controller script which is static so I can call it in this class. Any help would be greatly appreciated. Here is what I have:


    Code (CSharp):
    1.     float fireRateUpdate;
    2.     bool rapidFirePickUp;
    3.  
    4.     void Start()
    5.     {
    6.         fireRateUpdate = 0.15f;
    7.         rapidFirePickUp = false;
    8.     }
    9.  
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.tag == "Player" && rapidFirePickUp == false)
    13.         {
    14.             rapidFirePickUp = true;
    15.             RapidFireOn();
    16.             Destroy(gameObject);
    17.         }
    18.     }
    19.  
    20.     IEnumerator RapidFireOn()
    21.     {
    22.         PlayerController.fireRate = fireRateUpdate;
    23.         yield return new WaitForSeconds(10);
    24.         PlayerController.fireRate = 0.25f;
    25.         rapidFirePickUp = false;
    26.     }
    EDIT:
    Fixed code error pointed out by Fujik. Fire rate still does not update.
     
    Last edited: Feb 8, 2016
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    You're assigning the rate of the static fireRate to your local variable instead of the other way around.
     
    tburns517 likes this.
  3. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    Wow, what a silly mistake on my part. Fixed. Unfortunately this did not fix the issue.

    What happens in the RapidFireOn() function is that it changes the fire rate, allows the player to use this fire rate for 10 seconds, then goes back to the original fire rate. Am I using WaitForSeconds incorrectly?

    I did a bit more testing and I have narrowed it down to the issue being in the OnTriggerEnter() or RapidFireOn() functions.

    EDIT:

    Making some progress. Part of the issue was that the WaitForSeconds() method will not work unless you call it as a coroutine, so I now call the function as StartCoroutine(RapidFireOn()), and this effectively changes the fire rate. The problem now is the fire rate will not reset after 10 seconds has passed.
     
    Last edited: Feb 8, 2016
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    StartCoroutine(RapidFireOn()); instead of RapidFireOn();
    Also, bools are set to false by default. You don't need to set it to false manually in your start function.
     
    tburns517 likes this.
  5. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    I edited my post immediately with that same fix before you posted this. Good timing I suppose. As stated, the problem is now that it will not reset after 10 seconds.
     
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    You're destroying gameObject, which happens to be holding the instance of the class that's running your coroutine "RapidFireOn". Either hide it, disable its collider and destroy it once your 10 seconds is over, or make another class responsible for managing your powerup, so that it can still run when you destroy your powerup.
     
    tburns517 likes this.
  7. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    I can see exactly what is going on now. Regardless of whether I hide the object being picked up or not, it is being destroyed by the boundary, as it has its own script which destroys everything that is outside of it. So I feel the work around would be to give the pick up item a new location upon setting its render to false. It will stay in this location, inside the boundary, until the timer is up. I'm going to try that out and see if that works. If not, I'll report back.

    EDIT:
    Problem solved. So the work around is that the object will follow right behind the player (invisibly) until the 10 second timer runs out. It will then be destroyed by the RapidFireOn() function. The Update() function was added to change the position of the object when picked up, and to make sure the player is actually there to "hold onto it." I have to find the player first in Start(), since you can't attach a GameObject to a prefab. I'm new so I understand this may not be best programming practice, but I am making progress.

    The final code for those interested:

    Code (CSharp):
    1.     float fireRateUpdate;
    2.     bool rapidFirePickUp;
    3.     GameObject playerPosition;
    4.  
    5.     void Start()
    6.     {
    7.         playerPosition = GameObject.Find("Player");
    8.         fireRateUpdate = 0.15f;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         if (rapidFirePickUp == true && playerPosition != null)
    14.         {
    15.             gameObject.transform.position = new Vector3(playerPosition.transform.position.x, 0.0f, playerPosition.transform.position.z - 1);
    16.         }
    17.     }
    18.  
    19.     void OnTriggerEnter(Collider other)
    20.     {
    21.         if (other.tag == "Player" && rapidFirePickUp == false)
    22.         {
    23.             rapidFirePickUp = true;
    24.             gameObject.GetComponentInChildren<Renderer>().enabled = false;
    25.             StartCoroutine(RapidFireOn());
    26.         }
    27.     }
    28.  
    29.     IEnumerator RapidFireOn()
    30.     {
    31.         PlayerController.fireRate = fireRateUpdate;
    32.         yield return new WaitForSeconds(10);
    33.         PlayerController.fireRate = 0.25f;
    34.         rapidFirePickUp = false;
    35.         Destroy(gameObject);
    36.     }
    37. }
     
    Last edited: Feb 8, 2016