how to ADD reload system in zoombie shooter?
I have not done the zombie runner course, but I had a quick look at some things and have some suggestions (None of this is tested);
1. Weapon magazine size and use
First, your weapon will need a ‘magazine size’. This is how many bullets it can shoot before it needs to be reloaded. You can just add it to the Weapon
class, for example
[SerializeField] int magazineSize = 6;
Now, we only want to be able to shoot if we actually have enough bullets in our magazine, so we’ll also add a private variable to hold the current bullet count in the clip
private int currentAmmoAmount;
When we shoot, we’d want to check if we have any ammo in the clip before shooting. Update the Shoot
method to do this check
IEnumerator Shoot()
{
canShoot = false;
if (currentAmmoAmount > 0) // change this line
{
PlayMuzzleFlash();
ProcessRaycast();
currentAmmoAmount--; // reduce the clip
}
yield return new WaitForSeconds(timeBetweenShots);
canShoot = true;
}
2. Ammo store
Next, we have an ammo store that used to be our ‘magazine’, but now we will only reload from here. We need to update the ReduceCurrentAmmo()
function to give us all the ammo we need for the magazine. We will also have the function return how many bullets it is giving us because if we only have 4 bullets in the store and our magazine takes 6, we need to know so that we only reload with 4
public int ReduceCurrentAmmo(AmmoType ammoType, int amount)
{
// how many bullets can we get?
int actualAmount = Mathf.Min(amount, GetAmmoSlot(ammoType).ammoAmount);
GetAmmoSlot(ammoType).ammoAmount -= actualAmount; // subtract those
return actualAmount; // return
}
3. Reloading
Now we need to listen for the ‘reload’ key. Let’s assume it’s the ‘Tab’ key. We add this to the Update
method of the Weapon
class. I am adding a small delay in the reload. Reloading takes a little bit of time. We do this by defining how long a reload will take
[SerailizeField] float reloadTime = 2f; // two seconds to reload
private bool canReload = true;
void Update()
{
DisplayAmmo();
if (Input.GetMouseButtonDown(0) && canShoot == true)
{
StartCoroutine(Shoot());
}
if (Input.GetKeyDown(KeyCode.Tab) && canReload == true)
{
StartCoroutine(Reload());
}
}
IEnumerator Reload()
{
canReload = false;
yield return new waitForSeconds(reloadTime);
// start reloading
// how many bullets do we need?
var bulletsRequired = magazineSize - currentAmmoAmount;
// try to get all those from the store
currentAmmoAmount += ammoSlot.ReduceCurrentAmmo(ammoType, bulletsRequired);
canReload = true;
}
4. More
That’s it. There are a few things you may want to look at. I’ll leave those as challenges . Currently, you can shoot while reloading (and reload while shooting). Perhaps toggle canShoot
and canReload
in each of the coroutines, and check them before trying to run the coroutines. Also, you may want to change the ammo display. It will only show the full store’s amount atm, not the magazine. Nice little challenge there for you. There may be more issues but I didn’t test any of this, so I won’t know
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.