Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. If you're a macOS 10.13 High Sierra user take be sure to read this before updating Unity.
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Enemy fire Rate not working

Discussion in 'Scripting' started by jwalden, Nov 18, 2017.

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    36
    Hey guys, currently testing my enemy shooting. The fireRate is insanely fast however, and the code I've put in does not seem to let me control it.

    I've split the shooting mechanic into 2 segments:
    EnemyGunShoot
    &
    EnemyProjectileBehaviour

    Enemy ProjectileBehaviour
    Code (CSharp):
    1. public class EProjectileBehaviour : MonoBehaviour {
    2.  
    3.     public Rigidbody2D rb;
    4.     public float speed;
    5.     public GameObject projectile;
    6.     public GameObject projectileHolder;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.      
    12.  
    13.     }
    14.    
    15.  
    16.     void Update()
    17.     {
    18.         rb.AddForce(projectile.transform.up * speed * Time.deltaTime, ForceMode2D.Impulse);
    19.         DestroyObjectDelayed();
    20.     }
    21.  
    22.     void DestroyObjectDelayed()
    23.     {
    24.  
    25.         Destroy(projectileHolder, 3);
    26.     }
    27. }
    Enemy Gun Shoot
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class EnemyGunShoot : MonoBehaviour {
    5.  
    6.     public GameObject enemyProjectile;
    7.     public Transform eSOrigin;
    8.     public float fireRate;
    9.     public float damage;
    10.     float nextFire = 0;
    11.     public EnemyGunTrigger trigger;
    12.  
    13.     private void Update()
    14.     {
    15.         if (Time.time > nextFire)
    16.         nextFire = Time.time + fireRate;
    17.         GameObject pClone = Instantiate(enemyProjectile, eSOrigin.position, eSOrigin.rotation) as GameObject;
    18.        
    19.     }
    20. }
    Much of this code is very similar to my player shoot mechanic and the fireRate works just fine.

    Any advice would be greatly appreciated!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    4,068
    Not sure what you're doing in the Update method with the delayed destroy. Does that script exist on the instantiated projectile - i imagine so ?
    I would suggest, if that's the case, put :
    Code (csharp):
    1. void Start() {
    2.   Destroy(projectileHolder, 3);
    3.   }
    4.  
    and remove the method & method call (the one in update). The same effect will happen.

    Now, for your problem. You are missing braces :)
    Code (csharp):
    1.  
    2. if (Time.time > nextFire) { // without braces here, the instantiate always runs, because only 1 line after an 'if' is relevant.
    3.    nextFire = Time.time + fireRate;
    4.    GameObject pClone = Instantiate(enemyProjectile, eSOrigin.position, eSOrigin.rotation) as GameObject;
    5. }
    6.  
     
    GroZZleR likes this.
  3. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    36
    Ahhhh of course! Silly me. Thanks dude.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    4,068
    No problem :)