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): public class EProjectileBehaviour : MonoBehaviour { public Rigidbody2D rb; public float speed; public GameObject projectile; public GameObject projectileHolder; // Use this for initialization void Start () { } void Update() { rb.AddForce(projectile.transform.up * speed * Time.deltaTime, ForceMode2D.Impulse); DestroyObjectDelayed(); } void DestroyObjectDelayed() { Destroy(projectileHolder, 3); } } Enemy Gun Shoot Code (CSharp): using System.Collections.Generic; using UnityEngine; public class EnemyGunShoot : MonoBehaviour { public GameObject enemyProjectile; public Transform eSOrigin; public float fireRate; public float damage; float nextFire = 0; public EnemyGunTrigger trigger; private void Update() { if (Time.time > nextFire) nextFire = Time.time + fireRate; GameObject pClone = Instantiate(enemyProjectile, eSOrigin.position, eSOrigin.rotation) as GameObject; } } Much of this code is very similar to my player shoot mechanic and the fireRate works just fine. Any advice would be greatly appreciated!
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): void Start() { Destroy(projectileHolder, 3); } 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): if (Time.time > nextFire) { // without braces here, the instantiate always runs, because only 1 line after an 'if' is relevant. nextFire = Time.time + fireRate; GameObject pClone = Instantiate(enemyProjectile, eSOrigin.position, eSOrigin.rotation) as GameObject; }