I want the player to stop shooting the projectile when the ammo count reaches zero. But I can’t figure out how to do this when looking on the tutorial for this.
using UnityEngine;
using System.Collections;
public class ShipPlayerController : MonoBehaviour {
// The max ammo the player start with
[SerializeField] int MaxAmmo = 10;
// The current ammo remaining for the player
private int CurAmmo = 10;
// The number of projectiles shot
private int numShotsFired = 0;
// The number of enemies hit
private int numEnemyHits = 0;
// The reference to the bullet prefab
public Projectile Bullet;
void ResetStats() {
// MaxAmmo is pulic and set in the editor
CurAmmo = MaxAmmo;
// Tell the HUD to create Ammo count
PlayerHUD.updateAmmo(CurAmmo);
}
public void ModAmmo(int _value) {
CurAmmo += _value;
// Cap ammo from exceeding the max
if (CurAmmo > MaxAmmo)
CurAmmo = MaxAmmo;
// Update the new ammo value to the HUD
PlayerHUD.updateAmmo(CurAmmo);
}
void UpdateFiring() {
// Accumulate time each frame. When the fire key is pressed,
// we check if enough time has passed
FireTimer += Time.deltaTime;
// Detect if the fire button has been pressed
if (Input.GetButton("Fire1"))
{
if (FireTimer > FireRate)
{
// Reset the timer so it will start countin
// from scratch for the next shot
FireTimer = 0;
// Call the function which handles the spawning
DoWeaponFire();
print("The fire button has been pressed!");
}
}
}
// Handles the spawning of the projectile
void DoWeaponFire() {
print("The \"DoWeaponFire\" function has been called!");
// Create a new instance of the bullet and place it at the location
// of the player.
Instantiate(Bullet, transform.position, transform.rotation);
ModAmmo(-1);
}
}