- Home /
Controlling AI's Fire Rate
Hello everyone, I have a problem about my AI code. Normally, in my own character's shooting script, I control the rate of fire like this :
[code]function Update(){
if(Input.GetButtonDown("Fire1")){ if (waitTilNextFire <= 0 && mermiSayisi >0 ) {
bulletCount-=1; var bul = Instantiate(bullet,bSpawn.transform.position, bSpawn.transform.rotation); // this creates an object which has raycasting system, so my shooting is based on raycasting Instantiate(akShot,transform.position,Quaternion.identity); var muzzleFire = Instantiate(muzzleFlash,muzzleSpawn.transform.position, muzzleSpawn.transform.rotation); Destroy(muzzleFire,.03);
waitTilNextFire = 1;
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
}
} [/code]
But when I put this in AI code, I think it does not work well. I mean is there any good way to control fire rate of AI ? Actually, I need to control one line ;
[code] var bul = Instantiate(bullet,bSpawn.transform.position, bSpawn.transform.rotation);[/code]
this is the line which creates shooting, so I need to control it, any ideas ? Thanks:)
Answer by ScroodgeM · Jul 22, 2012 at 05:12 PM
C#
void SomeMethod() { //if it's time to fire and bullets more then zero StartCoroutine(ContinousFire()); } IEnumerator ContinousFire() { //insert here also others conditions to stop firing while (bulletCount > 0) { var bul = Instantiate(bullet,bSpawn.transform.position, bSpawn.transform.rotation); bulletCount--; //wait from 0.1 to 0.5 seconds before next shot yield return new WaitForSeconds(UnityEngine.Random.Range(0.1f, 0.5f)); } }
Your answer
Follow this Question
Related Questions
Fooling Rate Of Fire C# 1 Answer
Help in change the rate of fire. 1 Answer
audio rate changeing depending on axes 0 Answers
Rate of fire : explanation please 1 Answer