I am attempting to make a script where an enemy Ai is just stationary and he he fires 10 shots every X seconds. I have tried a few ways and have one working, but it seems a bit taxing on the iPhone.
The following code, that I thought relevant shows what works so far, but performance-wise, its not so great. It starts with the StationaryGunnerMode:
void StationaryGunnerMode() {
if (!amIDead)
//Gunner();
InvokeRepeating("Gunner", 2, .1f);
}
void Gunner () {
if (_stationaryGunnerCurrentAmmo == 0 ){
StartCoroutine("GunnerReload");
}
if (Time.time - stationaryGunnerFireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while( nextFireTime < Time.time && _stationaryGunnerCurrentAmmo != 0) {
StartCoroutine(ShootWeapon());
nextFireTime += stationaryGunnerFireRate;
_stationaryGunnerCurrentAmmo -= 1;
}
}
IEnumerator GunnerReload() {
yield return new WaitForSeconds(3f);
_stationaryGunnerCurrentAmmo = stationaryGunnerAmmo;
}
IEnumerator ShootWeapon () {
//shoots weapon routine
}
If anyone has any thoughts for an enemy AI to fire a ten round volley every few seconds in an efficient manner, your help would be appreciated. If you need more info to assist, please let me know.
Any thoughts?
Thanks