Search Unity

Enemy fire Rate not working

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

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    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:
    8,712
    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:
    62
    Ahhhh of course! Silly me. Thanks dude.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :)