Hello survivor! Welcome to Devlog #3 of Zombie Shooter.
In the previous DevLog, we have finished programming the zombie army. So today, let’s get geared up with some new weapons.
Check out my new gear
Originally, I planned for weapons such as a Sniper Rifle that has a strong shot that can pierce through multiple enemies, and a Grenade Launcher that shoots out deadly explosives. However, because my art assets only have models for a pistol, rifle, and a shotgun, I decided to scale down my game to only include these 3 weapons.
The idea with 3 different weapons is that:
Pistol: Semi-auto (player must click left mouse button for each shot) , basic weapon
Rifle: Full-auto (player can hold down the mouse button) , high ammo pool
Shotgun: Semi-auto, shoots four shots at a time
I have already made the pistol shooting mechanics previously. However, the pistol bullets shoot directly at where the player aims without any spread. Using this mechanic, the rifle shots would look boring. Also, by implementing a spread, I can just reuse the code to create shotgun bullets by shooting 4 bullets with a random spread at the same time.
Using a Rifle
if (Input.GetKey(KeyCode.Mouse0)) //rifle
{
RifleDelay -= Time.deltaTime;
if (RifleDelay <= 0.0 && RifleCurrentAmmo>0)
{
Instantiate(Bullet, bulletSpawnPoint.transform.position, Player.transform.rotation * Quaternion.Euler(0, 0, Random.Range(-5.0f, 5.0f)));
RifleDelay = 0.15f;
RifleCurrentAmmo--;
}
}
The code above creates a bullet at the position of the gun, with the rotation based on the rotation of the player and a random value (to create spread) every 0.15 seconds if the mouse is held down. It also reduces the rifle’s ammo by 1, which will be used for reload mechanics(more on that later).
Using a shotgun
ShotgunDelay -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Mouse0)) //rifle
{
if (ShotgunDelay <= 0.0 && ShotgunCurrentAmmo>0)
{
for (int i = 1; i <= 4; i++)
{
Instantiate(Bullet, bulletSpawnPoint.transform.position, Player.transform.rotation * Quaternion.Euler(0, 0, Random.Range(-5.0f, 5.0f)));
ShotgunDelay = 0.20f;
}
ShotgunCurrentAmmo--;
}
}
The code above is used for the shotgun. There are several differences compared to the code used for the rifle. First of all, the delay is not calculated in the if statement. This is because the delay needs to continue when the mouse is not held down, whereas the delay for the rifle would calculate as the mouse is held down.
Other than that, a for loop is used to instantiate 4 different bullets. Note that the decrease shotgun ammo statement is not the for loop, as we want each click would reduce the shotgun ammo by 1, not 4.
I’ve got an arsenal and I’m not afraid to use it
Now we need to add code to switch between the 3 weapons. To do that, I initialised an int value called Weapon to 1, and will increase up to 3 when E is pressed, and decrease down to 1 when Q is pressed. This int value controls what happens when the mouse button is pressed. Not only that, I can also use this value to change the model of the player based on what weapon is equipped.
Reloadin’
The aforementioned current ammo value is set to the max ammo of the weapon on startup.
private void Start()
{
PistolCurrentAmmo = PistolMaxAmmo;
RifleCurrentAmmo = RifleMaxAmmo;
ShotgunCurrentAmmo = ShotgunMaxAmmo;
}
When the current ammo is zero or when R is pressed, it would start a Coroutine, which is pretty much another method to start a delay. After the delay, it will set the weapon’s current ammo back to the weapon’s max ammo.
For example, this is how the pistol’s reload code looks like:-
IEnumerator reload(){
if (Weapon == 1)
{
if ((PistolCurrentAmmo <= 0 || (Input.GetKeyDown(KeyCode.R) && PistolCurrentAmmo!=PistolMaxAmmo)) && !isReloading)
{
isReloading = true;
yield return new WaitForSeconds(reloadTime);
if (isReloading)
{
PistolCurrentAmmo = PistolMaxAmmo;
isReloading = false;
}
}
}
}
And I just need to start the coroutine in void Update():
StartCoroutine(Reload());
If you’ve noticed the isReloading Boolean, that is to ensure that the player is still equipping the same weapon at the end of the reload. Although I did stop the Coroutine when the weapon is switched, I couldn’t get it to work effectively. So the Boolean is used to create an illusion that the reload is channeled.
I still haven’t added a stored ammo value that shows how much ammo the player has remaining, which would discourage players from only using the strong weapons. However, I hope to add this mechanic, and random ammo or drops from defeating enemies in the future.
Weapons here
I wanted the rifle and shotgun to be unlocked later on in the level. Thus, I made a weapon pickup that unlocks these weapons. It is basically a sprite that is destroyed when it collides with the player, and sets a Boolean called RifleEquipped or ShotgunEquipped to true. This Boolean is checked during weapon switching and the player can only switch to that weapon if that respective Boolean is true.
Armed to the teeth:
This is how the fully armed player looks like now:
And with that, we are now ready to take on the Enemies. Also, I am pretty much done with the gameplay of the game. What remains is the layout of the map, and a few ideas that I am still considering and researching. Thank you for sticking with me this far, and I hope you get to play the game soon!
Comments