- Home /
Ai can fire Infinite bullets?
So I made an Ai for a bullet hell and I tried to create a burst fire system where it has a certain amount of bullets it can fire, but has to reload for a few seconds before it can fire again. But for some reason, my AI can sometimes fire infinite bullets when you are in range... any ideas?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GruntAI : MonoBehaviour {
public float moveSpeed;
public float shootSpeed;
private bool inRange;
public float patrolArea;
private Transform playerPosition;
private Vector2 randPosition;
private Rigidbody2D rb;
public GameObject shot;
private Transform thisPos;
private bool CanShoot;
public float bullets;
public float maxBullets;
public float burstSpeed;
// Use this for initialization
void Start () {
playerPosition = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
inRange = false;
randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("generatePosition", 0, 1);
thisPos = GetComponent<Transform>();
CanShoot = true;
bullets = maxBullets;
}
// Update is called once per frame
void Update () {
if (inRange == true)
{
rb.velocity = (playerPosition.position - transform.position).normalized * moveSpeed * Time.deltaTime;
if (CanShoot == true && bullets > 0)
{
CanShoot = false;
bullets = bullets - 1;
Instantiate(shot, thisPos.position, Quaternion.identity);
StartCoroutine(reload());
}
} else
{
rb.velocity = (new Vector3(randPosition.x, randPosition.y, 0) - transform.position).normalized * moveSpeed / 2 * Time.deltaTime;
}
if (bullets < 1)
{
StartCoroutine(restoreBullets());
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
inRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
inRange = false;
}
}
public void generatePosition()
{
randPosition = new Vector2(Random.Range(-patrolArea, patrolArea), Random.Range(-patrolArea, patrolArea));
}
IEnumerator reload()
{
yield return new WaitForSeconds(shootSpeed);
CanShoot = true;
}
IEnumerator restoreBullets()
{
yield return new WaitForSeconds(burstSpeed);
bullets = maxBullets;
}
}
$$anonymous$$aybe shootSpeed is not initialized? So it gets set to it's default value 0. Try logging it or setting a breakpoint and inspecting the value. Same with burstSpeed.
Hi, I tried your script and it works well. What do you mean for infinite bullets? Does it shoot bullets every frame without waiting the shootSpeed value? Or doesn't it wait the burstSpeed for reloading?
it basically seems like it has infinite ammo for no reason. I should only be able to fire when bullets is greater than 0, so its weird. It waits the full reload and everything it just fires infinite once it reloads.
Answer by SlowCircuit · Jun 09, 2018 at 11:03 PM
You have:
if (bullets < 1) { StartCoroutine(restoreBullets()); }
in Update, which means as soon as they run out of bullets, it will start that coroutine every frame until bullets are increased. So you end up having a ton of coroutines running simultaneously all the time adding bullets.
One way to fix this is to add a bool to the mix. Something like "Restoring = true" before the delay in the coroutine, then change the above line to:
if (!Restoring && bullets < 1) { StartCoroutine(restoreBullets()); }
Yup, that was it. I knew it probably had something to do with that. Thanks!
Answer by Arisstephenson · Jun 09, 2018 at 10:28 PM
Are you sure you have shootSpeed
and burstSpeed
set in the editor?