不知道为什么会发生这种情况,但此代码允许在重新加载后发射超过 3 颗子弹。我试图找出原因。我认为这可能是检查哪个问题之间的时间,但我可能是错的。
任何帮助,将不胜感激。
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;
}