Hey guys! I’m almost done with my project but I need a last bit of help.
I got a script for my enemies to fire at random rate, but sometime even random can fail me.
I’d like a way to limit the number of bullet that can be instantiated at the same time on my screen.
For example, if five of my enemies shoot at the same time, the others won’t be able to shoot until those five bullets are gone. (I already have a timer for my bullets to vanish after some seconds)
here is my shooting script
using UnityEngine;
using System.Collections;
public class canonenemy : MonoBehaviour {
public GameObject bolt;
public Transform Canon;
public float fireRatep;
public float fireRatem;
private float nextFire = 0.0f;
// Use this for initialization
void Start () {
nextFire = nextFire + Random.Range (fireRatep, fireRatem);
}
// Update is called once per frame
void FixedUpdate () {
if (Time.time > nextFire) {
nextFire = Time.time + Random.Range (fireRatep, fireRatem);
Instantiate (bolt, Canon.position, Canon.rotation);
}
}
}
Not sure what can be done about it…
Thanks in advance!