Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Enemy fire Rate not working

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

  1. jwalden

    jwalden

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

    methos5k

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