Question by
Robertgold · Aug 29, 2017 at 11:27 AM ·
c#ienumeratorbugsreloadingweapon system
More bullets spawning that allowed
Hi,
Not sure why this happens but this code allows for more than 3 bullets to be fired after reloading. Am trying to work out why. I think it may be the time between checking which is this issues but I could be wrong.
Any help would be appreciated.
public bool isFiring;
public bool isReloading = false;
public BulletController bullet; // Reference another script
public float bulletSpeed; // bullet speed
public float timeBetweenShots; // time between shots can be fired
private float shotCounter;
public Transform firePoint;
public static int ammoRemaining = 3;
public static int maxAmmo = 3;
public Transform ammoText;
// Use this for initialization
void Awake () {
isReloading = false;
ammoRemaining = maxAmmo;
}
// Update is called once per frame
void Update () {
if(isFiring == true )
{
shotCounter -= Time.deltaTime;
if(shotCounter <= 0 && ammoRemaining > 0 && isReloading == false)
{
shotCounter = timeBetweenShots;
BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController; // creates a new instance of the bullet
newBullet.speed = bulletSpeed;
ammoRemaining -= 1;
ammoText.GetComponent<Text>().text = "Ammo:" + ammoRemaining;
}
}
else if (ammoRemaining == 0)
{
StartCoroutine(Reload());
}
else
{
shotCounter = 0;
}
}
public IEnumerator Reload()
{
isReloading = true;
ammoText.GetComponent<Text>().text = "REL...";
yield return new WaitForSeconds(2);
ammoRemaining = maxAmmo;
isReloading = false;
ammoText.GetComponent<Text>().text = "Ammo:" + ammoRemaining;
}
Comment
Your answer
Follow this Question
Related Questions
Bugs on move in air(falling); 0 Answers
Array index is out of range. c# 2 Answers
How to integrate threading with a www query for a UWP app? 1 Answer
IEnumerator instead of LateUpdate in unity 1 Answer
Trying to delay code, nothing works. 2 Answers